In version 2 of PowerShell, you could create hashtables, but the order in which the data is stored in the hashtable is not necessarily the same order you input it in. Take this example:
$hashTableV2 = @{a=1;b=2;c=3;d=4} $hashTableV2
Produces this output:
Name Value
---- -----
c 3
d 4
a 1
b 2
Version 3 of PowerShell now offers the ability to force the hashtable to hold the data in the same order it was created, using the new [ordered] tag.
$hashTableV3 = [ordered]@{a=1;b=2;c=3;d=4} $hashTableV3
The new syntax produces this output:
Name Value
---- -----
a 1
b 2
c 3
d 4
Now if it’s important that your hashtable is in a specific order, you have the [ordered] to make that happen.
Warning: This post was created using PowerShell v3 BETA. Changes to the final version may alter the validity of this post.
Do you have an example of when you would actually need an ordered hash table…?
Many years ago I worked on a system where we needed to import receipts / shipment transactions for our warehouse, these were generated from the third party application they used. The system spit out 24 files every day, one for each hour of the day. The file names were something like Warehouse-00.dat, Warehouse-01.dat, and so forth. The file names were always the same.
Obviously, we needed to be able to process the files in order, so as to get them in the proper time sequence. Loading the file names into an ordered hashtable would have allowed us to pass it to a function that would loop over the hashtable and process the files in order. It also would have made it easy to update the routine if they changed the names of the files.
(Which by the way did happen, the file names had the name of the warehouse and at some point the company renamed the warehouse which changed the name of the files.)
I have a cross-reference/pretty printer for Powershell that uses hash tables for the variable names. I sort it to get it into order since it runs under V2, but just by making it ordered, would help in the V3 version.
The [ordered] seems to be an unrecognized flag for me. Win 7 Pro, x86. I just downloaded the PS3, so version # is 2.7.1.1305.
Code structure is: $Fifty = [ordered]@{‘Date’=$date5000; ‘Denom’=$denom5000; ‘BegBal’=$BegBal5000}
it kicks back this error: ordered : Unable to find type [ordered]: make sure that the assembly containing this type is loaded.
+ CategoryInfo : InvalidOperation: (ordered:String) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
new-object : Cannot validate argument on parameter ‘Property’. The argument is null or empty. Supply an argument that is not null or empty and then try the command again
It might not have made the cut.
I have downloaded V3 Powershell. I am just loathe to install it, having experienced it under Windows 8 CP (THE worst Microsoft beta experience I’ve ever had) and the fact that some of my automation just plain stopped working, mainly due to the fact that I was always getting asked if I wanted to run the scripts, regardless of how I set things up. That probably is fixed, but I’m not moving until I’m forced more into it.