Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Associative Array - how to return mapped values?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 04-16-2008   #1 (permalink)
Jeffery Jensen
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 SpecsSystem Spec
Old 04-16-2008   #2 (permalink)
Kiron
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 SpecsSystem Spec
Old 04-16-2008   #3 (permalink)
Jeffery Jensen
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:
Quote:

> 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 SpecsSystem Spec
Old 04-16-2008   #4 (permalink)
Kiron
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 SpecsSystem Spec
Old 04-17-2008   #5 (permalink)
Kiron
Guest


 

Re: Associative Array - how to return mapped values?

Glad to help.

--
Kiron
My System SpecsSystem Spec
Closed Thread

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


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51