arrays


create array from string or variable


I'm trying to find the best way using PowerShell to create an array from the output of a variable.
I have the following code:
$ListNames = & "listNames.cmd" | Out-String
Which gives me the following output:
(c) 2016 - 2017 Use of this software is subject to license restrictions
::Microsoft Windows 7
::Microsoft Windows 7::name1
::Microsoft Windows 7::name2
::Microsoft Windows 7::name3
::Microsoft Windows 7::name4
::Microsoft Windows 7::name5
::Microsoft Windows 7::name6
::Microsoft Windows 7::name7
I need in an array everything from name1 thru name7.
I attempted without success trying to convert it to a CSV using delimiter :, tried creating a PSObject, and a few other ways that didn't get me the result I was looking for.
Any help or guidance would be greatly appreciated!
I would probably do something like this:
Remove the Out-String. You want to process the output line-by-line anyway, so merging the lines would only require you to split them again later.
Limit the output to relevant lines by piping it through a Where-Object filter (find or findstr would probably work too).
Split each remaining line at :: and pick the last field from each resulting array.
& ".\listNames.cmd" | Where-Object {
$_ -like '::*::*'
} | ForEach-Object {
($_ -split '::')[-1]
}
Another option would be a regular expression match as #TheMadTechnician suggested:
& ".\listNames.cmd" | Where-Object {
$_ -match '^::.+?::(.+)'
} | ForEach-Object {
$matches[1]
}

Related Links

Running Time of built in functions
Sum of matrices in 3-dimensional object of variable dimesions without looping
Quickly select arrays which have at least k elements common with master array
In Swift, how efficient is it to append an array?
How to correctly split a signal in matlab?
Accessing array of numbers in ARM (with Komodo compilation)
Load, show, convert image from byte array (database) in Windows Phone 8.1
Assembly - Copying an array of bytes - MIPS
Deep Query using MongoDB
plot array from matrix matlab [closed]
Function similar to head() in Matlab
Matlab, Symbolic functions,
R .Fortran return reshaped array?
Array in Powershell WON'T be written as an Array
Converting a graph structure to a 2d array
convert Array to object Angularjs [closed]

Categories

HOME
cluster-computing
answer-set-programming
meshlab
google-contacts
ubuntu-16.04
docker-swarm
angular2-directives
orientation
apple-numbers
dataframe
desktop
constructor
lotus-notes
packer
cockroachdb
xul
uiscrollview
activesync
cloudflare
uisplitviewcontroller
r-lavaan
marathon
uiautomator
scalajs-react
shopping-cart
metadata-extractor
vcard
configure
jboss5.x
postgresql-9.2
ifstream
ocs
redhat-datavirt
liquid-xml
parse-android-sdk
asmx
estimation
jtds
data-integration
openh264
gce
castle-dynamicproxy
emoticons
teiid
spyne
foxit
freetype2
django-filer
multiple-file-upload
search-regex
git-rebase
android-viewholder
geodjango
hornetq
oai
castle-windsor-3
graphical-logo
digits
criteria-api
httpie
named-ranges
livechat
collabnet
facebook-wall
svcutil.exe
edit-in-place
visual-c++-2010-express
real-time-updates
modeshape
osx-snow-leopard
windows-update
enumerators
osi
usn
flexicious
hashalgorithm
pureftpd
msn
webrat
winsnmp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App