|
| Associative Array - how to return mapped values? |
 |
Wed, 16 Apr 2008 17:19:00 -070 |
Hello PowerShellers,
How do I return a mapped value from an associative array?
I have this books.csv file:
ID,Author,Title
1,"Dr Seuss",Sneetches
2,Collins,"Good to Great"
3,Bolles,"What Color is Your Parachute"
Using PowerShell Import-csv cmdlet, I need to return the Title "Good to
Great" for ID=2. Here is my attempt:
PS C:\> $books = Import-CSV books.csv # populates the associate array
PS C:\> $books # everything looks good
PS C:\> Write-Host $books.2 # gives an error
PS C:\> Write-Host $books[2] # returns the 3rd record on
Bolles
PS C:\> Write-Host $books[2].Author # returns Bolles, not Collins
So, how do I query the associative array for ID=2 and return either the
Author or Title property?
Thanks for the help,
|
| Post Reply
|
| RE: Associative Array - how to return mapped values? |
 |
Wed, 16 Apr 2008 17:40:00 -070 |
The Microsoft TechNet article - What Can I Do With Windows PowerShell? Using
the Import-Csv cmdlet
(http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/import-csv.msp
x) shows how to select a record using the Where-Object
PS C:\> $books | Where-Object {$_.Author -eq "Collins"} #
returns record
but I still have the problem on how to just return the a single value or
column in the .csv file.
Also, I had no luck find the answer in the help file:
Get-Help about_Associative_Array
Thanks,
Jeff Jensen
"Jeffery Jensen" wrote:
> Hello PowerShellers,
>
> How do I return a mapped value from an associative array?
>
> I have this books.csv file:
>
> ID,Author,Title
> 1,"Dr Seuss",Sneetches
> 2,Collins,"Good to Great"
> 3,Bolles,"What Color is Your Parachute"
>
> Using PowerShell Import-csv cmdlet, I need to return the Title "Good
to
> Great" for ID=2. Here is my attempt:
>
> PS C:\> $books = Import-CSV books.csv # populates the associate array
> PS C:\> $books # everything looks
good
> PS C:\> Write-Host $books.2 # gives an error
> PS C:\> Write-Host $books[2] # returns the 3rd record on
> Bolles
> PS C:\> Write-Host $books[2].Author # returns Bolles, not Collins
>
> So, how do I query the associative array for ID=2 and return either the
> Author or Title property?
>
> Thanks for the help,
>
|
| Post Reply
|
| Re: Associative Array - how to return mapped values? |
 |
Wed, 16 Apr 2008 18:35:37 -060 |
In PowerShell arrays are zero based.
# filter the objects on a property that has a known value:
$books | where-object {$_.title -match 'Good to Great'}
# if you know the element's index and property
$books[1].title
# to view the elements members
$books | get-member
# to view the collection members
get-member -input $books
# or...
,$books | get-member
--
Kiron |
| Post Reply
|
| Re: Associative Array - how to return mapped values? |
 |
Wed, 16 Apr 2008 18:47:27 -060 |
Forgot to mention Where-Object's and Get-Member's aliases, '?' and 'gm'
respectively...
--
Kiron |
| Post Reply
|
| Re: Associative Array - how to return mapped values? |
 |
Wed, 16 Apr 2008 19:07:10 -060 |
Select-Object lets you choose which properties --or columns-- you want to
output, it also lets you create calculated properties. This Cmdlet returns
psObjects, they will have a header and their corresponding values. If you want
only values use ForEach-Object and get the properties from the current pipeline
object '$_'.
# for more on these Cmdlets
help select-object
help foreach-object
# selecting the Author column (header and value)
$books | select-object Author
# selecting just the values of the Author column
$books | foreach-object {$_.Author}
# filtering on Author and selecting the Title column (header and value)
$books | where-object {$_.Author -eq "Collins"} | select Title
# filtering and selecting the Title column's value, note the aliases
$books | ? {$_.Author -eq "Collins"} | % {$_.Title}
--
Kiron |
| Post Reply
|
|
|
|
|
|
|
|
|
|