Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

SOLVED: Predictable repeatable hash order in CORE

by VinsWorldcom (Prior)
on Sep 18, 2018 at 13:55 UTC ( [id://1222582]=perlquestion: print w/replies, xml ) Need Help??

VinsWorldcom has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE

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:

%hash = ( item1 => value, item2 => value, );

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.

Replies are listed 'Best First'.
Re: Predictable repeatable hash order in CORE
by choroba (Cardinal) on Sep 18, 2018 at 14:14 UTC
    Ordered keys in a JSON hash? Use canonical:
    use warnings; use strict; use feature qw{ say }; use JSON::PP; my $j = JSON::PP->new->canonical; say $j->encode({abc => 12, def => 13});
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: SOLVED: Predictable repeatable hash order in CORE
by Laurent_R (Canon) on Sep 18, 2018 at 15:46 UTC
    It might be sufficient to sort the keys (or possibly the values, depending on your needs) when you want to print out the hash content. Perhaps something like this:
    say "$_ => $hash{$_}" for sort keys %hash;
Re: SOLVED: Predictable repeatable hash order in CORE
by swl (Parson) on Sep 18, 2018 at 21:41 UTC

    Just a data point, but for cases where installation from CPAN is possible there is also Hash::Ordered.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1222582]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found