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

Separate string into array and then filter it [duplicate]
Protractor: How to store number text from div element
Weird results with parsing array from json in Swift3
How can I declare an array with diffrent type of data?
Trying to append content to numpy array
MongoDb $addFields inside arrays that $multiply two values within the array
bash: sed search and replace the path of array elements
Scala way for converting Long to ArrayByte
How to Assign Multidimensional Array Values to Tableview Cell swift 3
bring MC to stage with button
Extension file when called in not refreshing (swift3)
SAS - Transform dataset from wide to tall using arrays
How to remove duplicate value in an array Angular 2
How to remove specific object from array? [duplicate]
Index or create array of defined user defined type(VBA)
Ways to remove trailing commas

Categories

HOME
redis
npm
meshlab
vb6
qpython3
csvhelper
jscript
propertygrid
octopus-deploy
diagram
getorgchart
keystore
abcpdf
informatica-powercenter
bar-chart
rhapsody
movies
distance
jconsole
jquery-form-validator
tampermonkey
non-deterministic
visual-c++-2017
hash-collision
cep
system.data.sqlite
datadog
exponential
stocks
canvasjs
spreedly
color-scheme
npm-publish
dql
alchemy.js
android-preferences
socketscan
viewgroup
apiary
tableau-server
proof
broadcast
applepayjs
rhel6
workflow-foundation-4.5
cgcontext
slot
struts-layout
in-memory-database
laravel-query-builder
manifest.mf
multiple-file-upload
group-concat
fill
test-class
matcaffe
xjc
digits
crash-dumps
isml
apache-mina
shift-jis
guzzle6
consensus
jcr-sql2
microblaze
livechat
java-collections-api
humanizer
system.web
dache
bsp
kraken.js
help-viewer
anjuta
roxygen
bignum
va-list
swrl
localtime
argb
justgage
digital-design
template-haskell
cbcentralmanager
sequelpro
blockingqueue
couchdb-lucene
fotoware
visual-web-gui
jquery-selectbox
ajax.beginform
junit3
post-redirect-get
libavformat
hibernate3-maven-plugin
dirty-data
handheld
gears

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile