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

Output for merged array of hashes
mapping strings to array in MATLAB
Go-lang: How to convert slice to fixed size array?
C# Convert Json array to string array using Json.Net
Making a Matrix element equals the row number times the column number in matlab [duplicate]
Angular Js - ng-repeat over array of array objects
AS3 Flash - How to remove dynamically created MovieClip from different function?
access hash member of type array within array of hashes
Generating matlab array from union and replacement of two arrays
How to search an element in a sorted 2-D array with order (n)?
Ordering of array dimensions
Trying to declare an array of a certain size in Scala
Select pairs from an array (matching game) And add sound to that selection AS3 Flash CS4
Take array from one form and display contents in labels on another form
How do I display both the first and last elements of an array?
Is there any algorithm better than O(N) to find the first bit set in a bitarray that consists mostly of continous blocks?

Categories

HOME
rest
warnings
paramiko
translation
otrs
avro
heap-memory
snap.svg
netflix-feign
wysiwyg
flann
viber
squarespace
r-lavaan
jpeg2000
pycrypto
flexboxgrid
fabric
katharsis
skmaps
pentaho-report-designer
xilinx-ise
fileinfo
cell-array
shapes
nsurlconnection
readfile
jdbi
cppunit
scalafx
ti-basic
pagefile
pspice
hspec
galleriffic
bacnet
foxit
asteriskami
dojox.charting
brython
achievements
project-online
yoothemes
response-headers
visual-studio-code
xjc
criteria-api
subversion-edge
low-level
django-1.6
cpu-speed
cloudpebble
line-numbers
node-inspector
method-overriding
codeigniter-a3m
android-looper
map-force
help-viewer
resource-files
healthvault
sim900
onselect
dynamic-binding
symphony-cms
soa-suite
fraud-prevention
graph-coloring
htmltextwriter
device-emulation
sametime
lync-server-2010
sequelpro
caliper
unions
driving-directions
coff
printing-web-page
posting
gtk2hs
sitemappath
asp.net-mvc-controller
longjmp
dirty-data
project-lifecycle

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