|No such thing as a small change|
SOLVED: Predictable repeatable hash order in COREby VinsWorldcom (Prior)
|on Sep 18, 2018 at 13:55 UTC||Need Help??|
VinsWorldcom has asked for the wisdom of the Perl Monks concerning the following question:
I was so focused on an ordered hash, I didn't read the JSON::PP perldoc to see canonical() gives me what I want and due to the names of my hash keys, alphabetical works just fine. Thank you choroba for the quick response in Re: Predictable repeatable hash order in CORE.
Original node follows:
I see Tie::IxHash is not in core, but Tie::Hash is - which is the base class Tie::IxHash uses to create ordered hashes. Is there a way to get ordered hashes (predictable, repeatable order when printed for example) in CORE - other than rolling my own Tie::Hash extension (cause if I'm going that route, I might as well just install Tie::IxHash)?
Background (in case you're interested):
The background is I'm working with Python and non-developers using Linux so they'll have Perl installed but I can't count on them to install modules outside of CORE. We're using JSON objects for example, so I "use JSON::PP" since JSON.pm isn't in CORE. When I do a encode_json(\%hash), I'd like for the output to be predictable, repeatable order so the non-developers looking at a print ... output see everything nicely in order (the developers don't care, they can just take the JSON string into their Python scripts and parse away). So creating the %hash is pretty simple, no delete, insert, reorder ... the overhead from Tie::IxHash .. I just need to create a hash:
that can be predictable, repeatable order when output.
I've tested my code with Tie::IxHash and it works fine, so I may have to bite the bullet and teach a "even you can install Perl modules" course for my co-workers.