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

Row and Column Indices for 2-D array in Matlab
Perl reading file, printing unique value from a column
RISC String array
How do I increment my array counter to move to the next element in the array in this code?
Powershell: Add lines to a custom objects
Dimension-wise logical indexing in a 3D array
Referencing Ranges in Arrays
Matlab Cell Array Data
Rectangle.IntersetsWith() returning true whenever the two rectangles are on the same X axis
Array with string and number
For loop to take the value of the whole array each time
Match two files by column line by line - no key
Array resize on worksheet
find max values and change others to zero
Compare two array to get index of containing object
Difference in accessing variables in views

Categories

HOME
shopify
crate
raspbian
uibutton
mainframe
rfc
octopus-deploy
oclint
outlook-web-addins
ssr
camera-calibration
qt-installer
typeahead
pyyaml
boolean-expression
ejbca
k2
rworldmap
yosys
non-deterministic
branch
rhandsontable
cgal
ggmap
entity-system
fractals
rhomobile
xen
npm-publish
topic-modeling
twiki
socketscan
dart-pub
jupyter-console
rkt
double-buffering
chunked-encoding
slidesjs
date-range
rsqlite
pdflib
xib
picasa
instruments
nssplitview
ng2-material
emokit
gemini
faraday
proxygen
plone-3.x
windows-vista
code-readability
zuora
response-headers
built-in
twython
git-checkout
subversion-edge
code-first-migrations
jython-2.7
ejabberd-saas
temp-tables
candidate-key
node-imagemagick
java-collections-api
method-overriding
emma
pic24
visualstatemanager
help-viewer
mimosa
arangodb-php
bignum
cdata
nsmatrix
surveyor-gem
dwolla
qtgui
wpdb
pys60
android-4.0
mvccontrib
static-variables
gjs
zookeeper
text-size
scraperwiki
associative
server-error
svn-hooks
idictionary
soft-keyboard
economics
hardware-infrastructure

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