![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Associative Array - how to return mapped values? 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, Jeff Jensen |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Associative Array - how to return mapped values? 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 |
My System Specs![]() |
| | #3 (permalink) | ||||||||||||
| Guest | RE: Associative Array - how to return mapped values? The Microsoft TechNet article - What Can I Do With Windows PowerShell? Using the Import-Csv cmdlet (http://www.microsoft.com/technet/scr...mport-csv.mspx) 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:
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #4 (permalink) |
| Guest | Re: Associative Array - how to return mapped values? Sorry Jeff, I missed last part of your post. # psObjects (header and value) $books | ? {$_.id -eq 2} | select author $books | ? {$_.id -eq 2} | select author, title # just values $books | ? {$_.id -eq 2} | % {$_.author} $books | ? {$_.id -eq 2} | % {$_.title} # Write-Host only outputs to the console $test1 = $books | ? {$_.id -eq 2} | % {write-host $_.author - $_.title} # although you see the output $test is $null $test1 -eq $null $test1 # using string expansion and subexpression to # output a string object, the object is assigned to $test2 # no output to the console $test2 = $books | ? {$_.id -eq 2} | % {"$($_.author) - $($_.title)"} $test2 -eq $null $test2 -- Kiron |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Associative Array - how to return mapped values? Glad to help. -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to query the index value of associative array | Jeffery Jensen | PowerShell | 3 | 04-20-2008 09:07 AM |
| Getting all values out of an array of objects | casey.daniell | PowerShell | 6 | 02-22-2008 09:57 AM |
| how to assign values to array and how to create array via variable | Frank | PowerShell | 1 | 03-13-2007 05:18 PM |
| Initialize array without values | Frank | PowerShell | 11 | 03-02-2007 10:58 PM |
| Displaying an associative array | Andrew Watt [MVP] | PowerShell | 2 | 05-24-2006 08:40 AM |