Scripting Tips and Tricks: 'Jagged' Arrays


A jagged array is an array of arrays! Eh? Come again?

Well, in a jagged array, at least one 'compartment' or element contains a child array. Here’s what a simple jagged array (called $Array) might look like when sketched out:

1

2, 3

3

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

4

5, 5, 5

6

“Hello”, “World”

7

8

9

10

 

As you can see – it's rather spiky or jagged.

So… $Array[0] returns:

1

 

$Array[-1] returns:

10

 

$Array[7] returns:

Hello

World

 

$Array[7][-1] returns:

World

(…as ‘World’ is an element from a child array).

 

Here’s an example that creates a jagged array:

$ChildArrayElemenet1 = "Hello", "World", @{test="la"; brains = "blah"; cheese = "1,23,4,5,6,7"}

$ChildArrayElemenet2 = 1

$ChildArrayElemenet3 = 1,2,3

$ChildArrayElemenet4 = “Single”

$Array = $ChildArrayElemenet1, $ChildArrayElemenet2, $ChildArrayElemenet3, $ChildArrayElemenet4

  

Access the first element:

 

Access the hash table that’s the last element of the first child array:

 

 

Access the ‘test’ label in the hash table:

 

Let’s create a jagged PS Custom Object…

$Nic = gwmi -Class Win32_NetworkAdapter –property *

$Name = "Ian's Stuff"

$Stuff = [PSCustomObject]@{

Win32Nic = $Nic

Name = $Name

} #End of $Stuff

 

The Name property is a single string. The Win32Nic property is a child array.

 Now, access an element in the child array from the PS Custom Object…

 

Or…

 

 

Now, access some properties from an element in the child array…

 

HTH!

Comments (0)

Skip to main content