Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^2: Hashes do preserve insertion order after all

by kikuchiyo (Friar)
on Aug 01, 2019 at 16:10 UTC ( #11103718=note: print w/replies, xml ) Need Help??

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).

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103718]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-08-09 01:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found