On one-dimensional arrays, an array of indexes extracts multiple members.
gc tmp.csv | foreach { $_.split()[0,1] }
gc tmp.csv | foreach { $_.split()[3..5] }
$indexes = 1,3,5,7
gc tmp.csv | foreach { $_.split()[$indexes] }
This also works for hashtables:
PS (10) > @{a=1;b=2;c=3;d=4}["a","c"]
1
3
On n-dimensional arrays, you can also do this but the notation is more
complex. If you have an 2-d array in $data, then you could do
$data[(0,0),(1,1),(2,2)]
Composed indexing operations are for dealing with jagged arrays (arrays of
arrays). Here's an example:
PS (11) > $z = (1,2,3),(10,20,30),(100,200,300) # array of 3 elements,
each of which is an array of 3 elements
PS (12) > $z[1] # 2nd element
10
20
30
PS (13) > $z[1][1] # second element of the second element
20
PS (14) > $z[1][0] # first element of the second element
10
PS (15) > $z[1][2,3]
30
PS (16) > $z[1,2][0][2] # pathelogical - third element of the first element
of a slice of the second and third elements
30
- bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
"Marco Shaw" <marco@Znbnet.nb.ca> wrote in message
news:OeK7pgqCHHA.4772@TK2MSFTNGP02.phx.gbl...
>I want to take a CSV file, and do a split, but then I also want to print
>out the 1st and 2nd columns.
>
> Something like:
> gc tmp.csv|foreach{$_.split(',')[0] [1]} (which doesn't seem to work like
> I'd want it to)
> User1 50
> User3 100
> User1 20
>
> The raw data:
> PS C:\> gc tmp.csv
> User1,50
> User3,100
> User1,20
> PS C:\> gc tmp.csv|foreach{$_.split(',')[0]}
> User1
> User3
> User1
> PS C:\> gc tmp.csv|foreach{$_.split(',')[1]}
> 50
> 100
> 20
>
> Tried different combos to try to get both values together but the err
> out...
>
> Any ideas?
>