Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to SOLVED: Predictable repeatable hash order in CORE by VinsWorldcom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-25 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found