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

python 3 read array (list?) into new values
Decrypt an array of strings in vb.net
bash array with new line as IFS
create new object in array (which is in a object which is in an array which is document) in meteor/mongodb
Initializing an array of records in VHDL
Perl Array and Hash references Handling
Separating digits in decimal representation of integer
How do I return an array from an array function using groovy?
Perl - Reading two files to compare contents
Accessing hash values(arrays) in Perl
C - Errors in header file
Elasticsearch Term Filter on Array
Magento 1.7 left navigation category sort order alphabetically
Collection of Classes - Array property not populating correctly
Interview - find greater element for each array`s element
VB Array Error Grade Book Assignment

Categories

HOME
rest
vb6
wildcard
raspbian
facebook-graph-api
actionscript
value
triggers
currency
ng-admin
vuex
packer
ssr
xul
nuxt.js
ida
pdo
eclipse-luna
netezza
tree-traversal
jsonserializer
flexlm
plsqldeveloper
esql
kitematic
extractor
jquery-scrollify
visual-c++-2017
opencart2.3
pptp
dhtmlx-scheduler
protobuf-net
strophe
m2e
rhino
von-neumann
liquid-xml
topic-modeling
infusionsoft
code-composer
vcf
w3-total-cache
livefyre
gist
quintus
httplib2
qos
sql-import-wizard
hspec
mailcatcher
software-product-lines
wcf-ria-services
visible
zurb-foundation-apps
dojox.charting
logcat
distribute
concur
altbeacon
mptcp
risk-analysis
team-build
git-checkout
epl
belongs-to
viewflipper
grunt-contrib-concat
ember-components
iostream
sid
uipickerviewcontroller
magicalrecord-2.2
bigint
robospice
resgen
pep8
fileconveyor
vt100
scrollto
ninject-extensions
viewdidload
wchar
selectmanycheckbox
posting
cookieless
scalaxb
search-path
requestfactory
asp.net-routing
yajl
boost-date-time
nt4
system-codedom-compiler
ajaxpro
interface-design

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