Search

Ordered hashes as an option by Diogenus

Closed
as Fixed Help for as Fixed

3
0
Sign in
to vote
Type: Suggestion
ID: 412647
Opened: 2/8/2009 12:05:03 PM
Access Restriction: Public
0
Workaround(s)
I would be nice if Powershell, as an option, could use ORDERED HASHES similar to System.Collections.Specialized.OrderedDictionary.

It would be particularly useful to be able to initialize an ordered hash using the Powershell hash syntax.
Details (expand)
How often does this happen?
Always Happens
Have you seen this problem before in this product?
Yes, this happens in all previous versions
Reproduction Steps
No reproduction steps
Expected Results
No expected results
File Attachments
0 attachments
Sign in to post a comment.
Posted by Roman Kuzmin on 7/18/2011 at 9:46 PM
This is a very good news, thanks you. But the syntax looks awkward. And this is not a surprise that you have to explain allowed/not allowed issues.

Would it be less awkward and more concise to introduce and use yet another built-in notation, say:
@[one=1; two=2]
(just for example, I did not analyse possible drawbacks of this particular).
Posted by Stephen Mills on 7/18/2011 at 4:33 PM
Will the following work as it would be expected in the next version? If not please try to add it. I would hope it would default to [ordered], but hoping doesn't make it so.

New-Object PSObject -Property ([ordered]@{one=1;two=2} )

This is really where having the items ordered would be really good, especially since when scripting you frequently don't create format files and the order can be important.

Thanks and glad [ordered] was added.
Posted by Microsoft on 6/16/2010 at 11:43 PM
Hashtables in PowerShell are of type System.Collections.Hashtable. Insertion order is not preserved in hashtables. The next version of PowerShell supports the following syntax for creating a hashtable that preserves insertion order.

$ht = [ordered]@{one=1;two=2}

The word “ordered” is treated as an attribute on hashtable. The above statement will return an instance of System.Collections.Specialized.OrderedDictionary which preserves the insertion order.

Note that the new [ordered] syntax can only be used on a hashtable literal. Once a hashtable of type System.Collections.Hashtable is created, the insertion order is lost and creating an OrderedDictionary cannot restore the insertion order. For this reason, doing the following is meaningless
                
$ht = {one=”1”;two=”2”;three=”3”}
                
$od = [ordered]$ht

After the execution of the first statement, the insertion order is lost.

The following are some example statements and the expected results for those statements.

# Allowed. An instance of OrderedDictionary is created.
[ordered]@{one=1;two=2}

# Allowed, but ineffective. The first conversion retains the order. The second conversion (conversion to hashtable) does not throw an error. But the insertion order is lost.
[hashtable][ordered]@{one=1;two=2}

# Not allowed. The ordered attribute is allowed only in conjunction with a hashliteral.
[ordered][hashtable]@{one=1;two=2}

# Not allowed. Ordered can only be an attribute on a hashliteral.
[ordered]$x = @{one=1;two=2}
Sign in to post a workaround.