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

It's better to use array or object to store structured data?
Morton Encoding Z-indexing Space Usage
How to Remove Duplicate values in array in c#
Actionscript, hasOwnProperty() of multidimensional arrays
Labview : numbering color box array
Replace values of one column of matrix size 1*n to another matrix of size n*n
How to query a single embedded document in an array in MongoDB?
Pushing Sound files to an Array with Swift
Alamofire : Updating array's values with JSON's data
Issues with arrays, how would i go about solving this?
Traverse through an array with missing elements and report those missing elements in shell
Is it possible to send an object to a Chunk instead of an array
Need help on a Twig issue with associative arrays
is ruby assoc array a hashtable internally? what is lookup time complexity?
Like the .assoc method when working with multidimensional arrays in Ruby
Lisp make-array from user input incorrect length

Categories

HOME
batch-processing
udp
erlang
answer-set-programming
filterrific
wmic
raspbian
matplotlib
codeeffects
specflow
rebol
devstack
docker-windows
websphere-liberty
subquery
eval
onenote-api
getorgchart
cakephp-2.9
spinnaker
uiscrollview
multiple-monitors
jquery-terminal
bootstrap-popover
scenebuilder
rhapsody
anychart
eclipselink
styles
kadanes-algorithm
shippo
flexboxgrid
apply
jquery-waypoints
nmf
tampermonkey
pipelinedb
unpack
hana-studio
jackson-dataformat-csv
svnkit
viewstate
honeysql
swagger-editor
android-download-manager
freetts
react-native-router-flux
fastq
tiddlywiki
rollback
decompress
etherpad
static-code-analysis
opencpu
redux-router
vici
rsqlite
pspice
inject
master
windows-azure-pack
tomee
instruments
multifile-uploader
gradle-eclipse
gemini
inet
fortran90
multinomial
retro-computing
on-duplicate-key
viper-architecture
criteria-api
rspec3
jython-2.7
cpan
rhel5
cocoascript
left-recursion
omnifocus
and-operator
node-inspector
author
boost-test
funkload
broadcasting
hamsterdb
android-4.0
attachevent
xgettext
signals2
port-scanning
server-error
search-path
rijndael
pantheios
savestate
filetime
web-statistics
dotproject

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