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

Non contiguous ranges into monodimensional array
How to convert an array to a hash with specified common values
How can I find all the cells that have the same values in a multi-dimensional array in octave / matlab
Collecting Multiple Properties Of An Object To An Array
Sublime regex comma replace character
textscan returning empty cell array
Is there a way to initialise an array using an expression/function in R
How are these arrays being used in this Fortran algorithm?
How to assign an array to a row of 2d array in Scala
logstash extract matched strings into array
A class contains an array of objects. Do I need to set each of them to nothing on terminating the containing class?
remove items from array not working
Split Function using an Excel row as delimiter
AS3 Array issue and Arduino
Google apps script - get A1 cell notation of each element in an array
find lowest value for each element given some intervals and its values

Categories

HOME
jenkins-plugins
puzzle
jar
label
transparent
rebol
tizen-wearable-sdk
urlencode
vsftpd
wysiwyg
tee
data-analysis
spring-cloud-config
phpbb
cairo
pyyaml
grub2
tree-traversal
primes
vs2017
jni
cvs2svn
pptp
aurelia-binding
red-black-tree
gpib
helix-3d-toolkit
stocks
ios-ui-automation
windows-10-iot-core
precedence
codesys
floating-accuracy
code-composer
constants
powermta
resuming-training
z3py
tuleap
ti-basic
web-deployment-project
mbaas
static-code-analysis
shinyjs
libtiff.net
pdflib
galleriffic
vst
shipitjs
dbscan
uwp-maps
picking
distribute
spservices
test-class
cctray
function-fitting
app.xaml
pyrocms
type-mismatch
sparse-file
apache-mina
google-plus-one
adodb
c++-actor-framework
utf-32
line-numbers
gyroscope-framework
and-operator
x-ua-compatible
google-admin-audit-api
collabnet
genetic-programming
coin-flipping
ssms-addin
ifft
ksoap2
visual-c++-2010-express
mimosa
localtime
ienumerator
argb
surveyor-gem
soa-suite
htmltextwriter
process-monitor
domain-calculus
cbcentralmanager
returnurl
javascriptserializer
mysql-error-1045
fileutils
nsindexpath
qtembedded
opengl-es-lighting
ruby-debug
visual-c++-2008-express
openvg
weborb





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