http://qs321.pair.com?node_id=11103718


in reply to Re: Hashes do preserve insertion order after all
in thread Hashes do preserve insertion order after all

All the criticism against this hack brought up in the other replies is valid.

I see one context where it might still have a viable use case: in very short, one shot, one liner scripts that build one data structure, transform or filter it some way then print the result. This is the context where I've discovered it by the way.

Example: I had a complex, multilevel JSON, and I wanted a flat list of some of the values from a sub-hash. The json_xs command line script from the JSON::XS module has a mode to execute arbitrary code before printing the result, so I used the following one liner:

json_xs -t string -e 'my $x; for (sort values %{ $_->{database}->{data +bases} }) {$x .= $_->{dbname}. $/;} $_=$x' < conf/test.json

In this (and only this) context this hack can be used quite reliably, and it has the advantage that it is concise and does not require any external modules (and presumably it is fast, because all it uses is the built-in reference stringification and sort's default lexical sorting).