|
|
||||||
|
#1
|
|
|
|
|
Hello PowerShellers,
How do I return the index value of an associative array? I'm referencing "Pro Windows PowerShell" by Hristo Deshev, p. 18-24 Problem 1: Array and Collection PS C:\> $a = (1,2,3) PS C:\> $a[2] # return the value 3 since PowerShell array indexs start at 0 PS C:\> $a = @("Solaris","Windows","Mac") # works How would I query $a for "Windows" and return the array index, which is 1? Problem 2: Dictionaries and Hash Tables PS C:\> $b = @{1="Solaris"; 2="Windows", 3="Mac"} # doesn't work PS C:\> $b = @{1 = "Solaris"; 2 = "Windows", 3 = "Mac"} # works PS C:\> $b.Keys # returns 3,2,1 PS C:\> $b.Values # returns Mac, Windows, Solaris PS C:\> $b[0] # doesn't return anything, must not use an array index PS C:\> $b[2] # returns Windows PS C:\> $b["Windows"] # doesn't return 2, why? How do I query $b using the value to determine the name/key? Problem 3: Import-CSV cmdlet (not sure if technical name is associative array) Given the following CSV file called os.csv ID,OS,Company 1,Solaris,Sun 2,Windows,Microsoft 3,Mac,Apple PS C:\> $c = Import-Csv os.csv PS C:\> $c # will return a table view of os.csv PS C:\> $c.Keys # doesn't return anything PS C:\> $c.Values # doesn't return anything PS C:\> $c[1] # returns the record - 2,Windows,Microsoft PS C:\> $c[-1] # returns the last record - 3,Mac,Apple PS C:\> $c[0..2] # returns all records PS C:\> $c[0..-1] # doesn't return all records, why? Just returns 1st and last PS C:\> $c | Select-Object OS # will return the OS Field/Column PS C:\> $c | Where-Object {$_.OS -eq "Windows"} # will return record 2,Windows,Microsoft So how do I query $c to return the index of the associative array? Thanks for the help, Jeff |
|
|
|
#2
|
|
|
|
|
# 3: Array of psObjects
$c = ipcsv x\os.csv # get the index for each element whose OS property is -eq to $pattern $i = 0..($c.length - 1) | ? {$c[$_].OS -eq $pattern} # retrieve the element(s) using _array_ notation $c[$i].OS -- Kiron |
|
#3
|
|
|
|
|
Jeff,
There is no direct mapping from a value in an array, hashtable, CSV file, database, ... to the index, key, "noteproperty", query in any language that I'm familiar with. Values are not unique only keys, indices, ... If you really need to look both ways one idea in Powershell is to assoicate a noteproperty with the value that equals the index. Kiron has another solution which is simpiler and likely better for most cases. "Jeffery Jensen" wrote: [..] |
|
|
| Similar Threads | |
| Regular Expression Result as Associative Array Index Hi Everyone. $layout=ereg_replace("\[\[([a-z:0-9]+)\]\]", $dillo['\\1'], $layout); I'm trying to use the result of a Regular Expression Match as an Index for the... |
|
| Returning WMI query data as an associative array? I'm learning WMI scripting coming from a PHP background, and I'm looking for something to point me in the right direction. When I return a query in PHP, I can read the... |
|
| Data structure for fast associative array with lookup by both key and index Hi all, I'm looking for a data structure for an associative array with the following properties: - Maintains key/value pairs. - Remembers the sequence order in which the... |
|
| finding the index name of an associative array myarray=array() myarray['a']=1 myarray['b']=1 myarray['c']=1 Is there an iterative way to find out the array index values ('a', 'b' and 'c') of myarray? |
|
| Help with SQL query and associative array I am using a SQL query to pull data that I need for a report. I can get it to print out just fine on screen, but I can't figure out how to get the data into an associative... |
|
|
All times are GMT. The time now is 03:31 PM. | Privacy Policy
|