Complex Data Structures

  • Thread starter rchiav@gmail.com
  • Start date
R

rchiav@gmail.com

Guest
I've been looking for a little while today but I haven't been able to
find any documentation on complex data structures with Powershell.
What I'm wondering is, can you create arrays nested in associative
arrays which are also nested in associative arrays, etc? Or is there a
more Powershellish way to store nested data?

 


Try this:

$array1 = , 2
$array2 = 1, 3, 5, 7 ,9
$array3 = 'abc', 'def', 'xyz'
$hash1 = @{
'EmptyArray' = @();
'Singleton' = $array1;
'OddNumbers' = $array2;
'LetterGroups' = $array3
}
$hash2 = @{
'Hash1' = $hash1;
'Empty Hash' = @{}
}

# always quote the key name when using array indexing
$hash2.Hash1['Singleton']

# use string variable expansion to cast the array as [String]
"$($hash2.Hash1.LetterGroups)"

# get the fourth element of OddNumbers array
$hash2.Hash1.OddNumbers[3]

# if key name contains spaces use quotes
$hash2.'Empty Hash'
$hash2.Hash1
$hash2

Read about Hash tables:
help about_Associative_Array

Read about Arrays:
help about_Array

# - - - - - - - - #

PowerShell les you create custom objects with Add-Member:

$collection1 = @()
$object = New-Object psObject
# using parameter names
Add-Member -MemberType NoteProperty -Name 'Empty Array' -Value @() -InputObject $object

# passing first three parameters by position
Add-Member NoteProperty Singleton @(, 2) -In $object

# using wildcard in -MeberType parameter
Add-Member N* OddNumbers 1, 3, 5, 7 ,9 -In $object

# using the int value of -MemberType parameter
Add-Member 8 LetterGroups 'abc', 'def', 'xyz' -In $object
$collection1 += $object

$object = New-Object psObject
Add-Member N* Empty $null -In $object
Add-Member 8 Collection1 $collection1 -In $object
$collection2 = @(), $object

$collection2

# using array indexing
$collection2[1].collection1[0].Singleton

# use string variable expansion to cast the array as [String]
"$($collection2[1].collection1[0].LetterGroups)"

# get the fourth element of OddNumbers array
$collection2[1].collection1[0].OddNumbers

--
Kiron
 
Excuse the typo. Last line should be:
$collection2[1].collection1[0].OddNumbers[3]

--
Kiron
 
Thanks for the response, but I don't see message prior to this.


On Dec 3, 3:09 pm, "Kiron" <Ki...@HighPlainsDrifter.com> wrote:
> Excuse the typo. Last line should be:
> $collection2[1].collection1[0].OddNumbers[3]
>
> --
> Kiron


 
Sending original, no typo here :)

Try this:

$array1 = , 2
$array2 = 1, 3, 5, 7 ,9
$array3 = 'abc', 'def', 'xyz'
$hash1 = @{
'EmptyArray' = @();
'Singleton' = $array1;
'OddNumbers' = $array2;
'LetterGroups' = $array3
}
$hash2 = @{
'Hash1' = $hash1;
'Empty Hash' = @{}
}

# always quote the key name when using array indexing
$hash2.Hash1['Singleton']

# use string variable expansion to cast the array as [String]
"$($hash2.Hash1.LetterGroups)"

# get the fourth element of OddNumbers array
$hash2.Hash1.OddNumbers[3]

# if key name contains spaces use quotes
$hash2.'Empty Hash'
$hash2.Hash1
$hash2

Read about Hash tables:
help about_Associative_Array

Read about Arrays:
help about_Array

# - - - - - - - - #

PowerShell les you create custom objects with Add-Member:

$collection1 = @()
$object = New-Object psObject
# using parameter names
Add-Member -MemberType NoteProperty -Name 'Empty Array' -Value @() -InputObject $object

# passing first three parameters by position
Add-Member NoteProperty Singleton @(, 2) -In $object

# using wildcard in -MeberType parameter
Add-Member N* OddNumbers 1, 3, 5, 7 ,9 -In $object

# using the int value of -MemberType parameter
Add-Member 8 LetterGroups 'abc', 'def', 'xyz' -In $object
$collection1 += $object

$object = New-Object psObject
Add-Member N* Empty $null -In $object
Add-Member 8 Collection1 $collection1 -In $object
$collection2 = @(), $object

$collection2

# using array indexing
$collection2[1].collection1[0].Singleton

# use string variable expansion to cast the array as [String]
"$($collection2[1].collection1[0].LetterGroups)"

# get the fourth element of OddNumbers array
$collection2[1].collection1[0].OddNumbers[3]

--
Kiron
 
On Dec 3, 3:47 pm, "rch...@gmail.com" <rch...@gmail.com> wrote:
> Thanks for the response, but I don't see message prior to this.
>
> On Dec 3, 3:09 pm, "Kiron" <Ki...@HighPlainsDrifter.com> wrote:
>
>
>
> > Excuse the typo. Last line should be:
> > $collection2[1].collection1[0].OddNumbers[3]

>
> > --
> > Kiron- Hide quoted text -

>
> - Show quoted text -


I don't see Kiron's first post either? (using google groups here), but
I'm sure it was something like this:

Yes, you can. ;-)

Nested arrays:

ps> $arr = @(1,(1,2,3))
ps> $arr.length
2
ps> $arr[1].length
3

Nested hashtables:

ps> $hash = @{ foo = @{ bar = "test"; foo2 = "test2" }}
ps> $hash.keys
foo
ps> $hash["foo"].keys
bar
foo2

Hope this helps,

- Oisin / x0n



 

Back
Top