Problem sorting multidimentionnal hash on insertion order

Discussion in 'Perl Misc' started by Sébastien Cottalorda, Feb 6, 2006.

  1. Hi all,

    I encoutered a problem trying to sort a hash of hash on insertion order.

    Here is the code:

    #!/usr/bin/perl -w
    use strict;
    use Tie::IxHash;
    no strict 'subs';
    my %hash = ();
    my %complex_hash = ();
    die 'hash Tie Impossible' unless tie %hash,'Tie::IxHash';
    die 'complex_hash Tie Impossible' unless tie %complex_hash,Tie::IxHash';
    #======== First Part OK ======================
    $hash{two} = 2;
    $hash{one} = 1;
    $hash{five} = 5;
    $hash{three} = 3;
    $hash{four} = 4;
    print "$_ => $hash{$_}\n" foreach keys %hash;
    print "--------------------------------------\n";
    #======== Second Part not OK ==================
    $complex_hash{value}{two} = 2;
    $complex_hash{value}{one} = 1;
    $complex_hash{value}{five} = 5;
    $complex_hash{value}{three} = 3;
    $complex_hash{value}{four} = 4;
    foreach (keys %{ $complex_hash{value} }){
    print "$_ => $complex_value{value}{$_}\n";
    }
    exit;

    The first part is sorted correctly.
    That's not the case for the second one.

    If someone as a clue ?

    Thanks in advance.

    Sébastien
     
    Sébastien Cottalorda, Feb 6, 2006
    #1
    1. Advertising

  2. Sébastien Cottalorda

    Guest

    Sébastien Cottalorda wrote:
    > die 'complex_hash Tie Impossible' unless tie %complex_hash,Tie::IxHash';


    Try

    die 'oops - $!' unless tie %{$complex_hash{value}}, 'Tie::IxHash';

    --
    http://DavidFilmer.com
     
    , Feb 6, 2006
    #2
    1. Advertising

  3. Sébastien Cottalorda

    Paul Lalli Guest

    Sébastien Cottalorda wrote:
    > #!/usr/bin/perl -w
    > use strict;
    > use Tie::IxHash;
    > no strict 'subs';
    > my %hash = ();
    > my %complex_hash = ();
    > die 'hash Tie Impossible' unless tie %hash,'Tie::IxHash';
    > die 'complex_hash Tie Impossible' unless tie %complex_hash,Tie::IxHash';


    syntax error.

    > #======== First Part OK ======================
    > $hash{two} = 2;
    > $hash{one} = 1;
    > $hash{five} = 5;
    > $hash{three} = 3;
    > $hash{four} = 4;
    > print "$_ => $hash{$_}\n" foreach keys %hash;
    > print "--------------------------------------\n";
    > #======== Second Part not OK ==================
    > $complex_hash{value}{two} = 2;
    > $complex_hash{value}{one} = 1;
    > $complex_hash{value}{five} = 5;
    > $complex_hash{value}{three} = 3;
    > $complex_hash{value}{four} = 4;
    > foreach (keys %{ $complex_hash{value} }){
    > print "$_ => $complex_value{value}{$_}\n";


    wrong variable.

    > }
    > exit;
    >


    Please take more care composing a post to Usenet. Copy and paste your
    code. Do not retype.

    > The first part is sorted correctly.
    > That's not the case for the second one.


    %complex_hash is a tied hash.
    %{$complex_hash{value}} is a normal hash.

    Add this line before any of the $complex_hash{value} insertions.:
    die 'complex_hash{value} Tie Impossible' unless tie
    %{$complex_hash{value}}, 'Tie::IxHash';

    Paul Lalli
     
    Paul Lalli, Feb 6, 2006
    #3
  4. Sébastien Cottalorda

    Guest

    wrote:
    > die 'oops - $!' unless tie %{$complex_hash{value}}, 'Tie::IxHash';


    arrg - doublequotes on the die() message...

    die "oops - $!" unless tie %{$complex_hash{value}}, 'Tie::IxHash';
     
    , Feb 6, 2006
    #4
  5. Sébastien Cottalorda <> wrote in
    news:43e7b28f$0$17074$:

    > #!/usr/bin/perl -w
    > use strict;
    > use Tie::IxHash;
    > no strict 'subs';


    Why?

    > my %hash = ();
    > my %complex_hash = ();
    > die 'hash Tie Impossible' unless tie %hash,'Tie::IxHash';
    > die 'complex_hash Tie Impossible' unless tie
    > %complex_hash,Tie::IxHash';
    > #======== First Part OK ======================
    > $hash{two} = 2;
    > $hash{one} = 1;
    > $hash{five} = 5;
    > $hash{three} = 3;
    > $hash{four} = 4;
    > print "$_ => $hash{$_}\n" foreach keys %hash;
    > print "--------------------------------------\n";
    > #======== Second Part not OK ==================
    > $complex_hash{value}{two} = 2;
    > $complex_hash{value}{one} = 1;
    > $complex_hash{value}{five} = 5;
    > $complex_hash{value}{three} = 3;
    > $complex_hash{value}{four} = 4;
    > foreach (keys %{ $complex_hash{value} }){
    > print "$_ => $complex_value{value}{$_}\n";
    > }
    > exit;
    >
    > The first part is sorted correctly.
    > That's not the case for the second one.
    >
    > If someone as a clue ?


    $complex_hash{value} is a reference to just a regular hash table.

    #!/usr/bin/perl

    use strict;
    use warnings;

    use Tie::IxHash;

    my %complex_hash;
    tie %complex_hash, 'Tie::IxHash', (One => {}, Two => {});

    for my $k (keys %complex_hash) {
    tie %{ $complex_hash{$k} }, 'Tie::IxHash';
    $complex_hash{$k}->{A} = 1;
    $complex_hash{$k}->{B} = 2;
    }

    for my $k (keys %complex_hash) {
    print "$k\n";
    for my $kk (keys %{ $complex_hash{$k} }) {
    print "\t$kk => $complex_hash{$k}->{$kk}\n";
    }
    }

    __END__


    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 6, 2006
    #5
  6. Sébastien Cottalorda

    Guest

    Sébastien_Cottalorda <> wrote:
    > Hi all,
    >
    > I encoutered a problem trying to sort a hash of hash on insertion order.



    use Tie::IxHash::Easy;

    (I sat down and wrote my own module to do this, then I realized I should
    have searched CPAN first. Sure enough, there it was, almost identical to
    mine.)


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 7, 2006
    #6
  7. Paul Lalli a écrit :


    >>die 'complex_hash Tie Impossible' unless tie %complex_hash,Tie::IxHash';

    >
    >
    > syntax error.


    Right!


    >>foreach (keys %{ $complex_hash{value} }){
    >> print "$_ => $complex_value{value}{$_}\n";

    >
    >
    > wrong variable.


    Right!

    >
    >>}
    >>exit;
    >>

    >
    >
    > Please take more care composing a post to Usenet. Copy and paste your
    > code. Do not retype.


    You're true.
    In fact I Copy/Paste my code but I make mistake translating french
    syntax variables to english.
    Sorry.

    >
    >>The first part is sorted correctly.
    >>That's not the case for the second one.

    >
    >
    > %complex_hash is a tied hash.
    > %{$complex_hash{value}} is a normal hash.


    I understand now.

    > Add this line before any of the $complex_hash{value} insertions.:
    > die 'complex_hash{value} Tie Impossible' unless tie
    > %{$complex_hash{value}}, 'Tie::IxHash';
    >
    > Paul Lalli


    Thanks for your help.

    Cheers.

    Sebastien
     
    Sébastien Cottalorda, Feb 7, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tanguy Fautré

    std::multimap insertion order guarantees

    Tanguy Fautré, Oct 5, 2003, in forum: C++
    Replies:
    13
    Views:
    849
    David B. Held
    Oct 6, 2003
  2. Mosfet
    Replies:
    6
    Views:
    5,114
  3. rp
    Replies:
    1
    Views:
    597
    red floyd
    Nov 10, 2011
  4. Bob Hutchison
    Replies:
    8
    Views:
    177
    Bob Hutchison
    Sep 28, 2005
  5. Joachim (München)

    Hash insertion order preservation in ruby1.9

    Joachim (München), Mar 31, 2008, in forum: Ruby
    Replies:
    4
    Views:
    133
    Joachim (München)
    Apr 10, 2008
Loading...

Share This Page