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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello Monks,

I was wondering if anyone knows if its possible to use Tie::IxHash (or something similar) in such a way that all hashes retain their key order.


My situation is this:

I've got a text file that needs modifying, and it is structured remarkably similar to a hash of hashes (I just need to search and replace '()' with '{}' and '=' with '=>'). Because of the structure, creating a hash with a 'do' statement is very easy. The problem is, after I've performed the necessary changes, I'd like to output the file again in the same order. Usually when I need to preserve the key order for a hash, I use 'Tie::IxHash', but since I'm creating the hash of hashes from a file, I can't tie second level of hashes with 'Tie::IxHash'.


E.g.
Script:
use Data::Dumper; use Tie::IxHash; tie %data, 'Tie::IxHash'; do "File"; $data{SomeMoreStuff}{SomeMoreData} = 'x'; print Dumper \%data;
File:
%data = ( SomeStuff => { SomeData => 'a', SomeMoreData =>'b', OtherData =>'c', }, SomeMoreStuff => { SomeData =>'a', SomeMoreData => 'b', SomeExtraData => 'c', OtherData => 'd' } );
The output looks like:
$VAR1 = { 'SomeStuff' => { 'OtherData' => 'c', 'SomeMoreData' => 'b', 'SomeData' => 'a' }, 'SomeMoreStuff' => { 'OtherData' => 'd', 'SomeMoreData' => 'x', 'SomeExtraData' => 'c', 'SomeData' => 'a' } };
The output I want:
$VAR1 = { 'SomeStuff' => { 'SomeData' => 'a' 'SomeMoreData' => 'b', 'OtherData' => 'c', }, 'SomeMoreStuff' => { 'SomeData' => 'a' 'SomeMoreData' => 'x', 'SomeExtraData' => 'c', 'OtherData' => 'd', } };

Anyone have any thoughts on how to tie the second level hashes without needing to parse the file line by line and tie each hash as its created?

Thanks!

In reply to Tie a hash of hashes? by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (12)
    As of 2014-09-02 15:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (25 votes), past polls