When working with HASH tables, sometimes they display a little bit of a randomization problem. Take a look:
1 2 3 4 5 6 7 8 | $Hash = ConvertFrom-StringData @' Apple=Red Orange=Orange Banana=Yellow Pear=Green Blueberry=Blue Plum=Purple '@ |
$Hash
Name Value
---- -----
Pear Green
Plum Purple
Apple Red
Blueberry Blue
Banana Yellow
Orange Orange
The output is not in the same order that I provided when I created the hash table. This is where PowerShell’s Ordered Dictionary helps. Take a look at the change in the code and the output.
1 2 3 4 5 6 | $Hash = [Ordered]@{"Apple"="Red"; "Orange"="Orange"; "Banana"="Yellow"; "Pear"="Green"; "Blueberry"="Blue"; "Plum"="Purple"} |
$Hash
Name Value
---- -----
Apple Red
Orange Orange
Banana Yellow
Pear Green
Blueberry Blue
Plum Purple
Just a few things to note. You must placed the [Ordered] attribute in front of the @ or it will not work. You also need at least PowerShell V3. Is there any advantage to using an ordered list? I would say this is just if you plan to display the hash table. Otherwise, it works just the same as a none ordered hash table.
Comments