Using Tie::IxHash order a hash reference

Discussion in 'Perl Misc' started by Darius, Feb 11, 2004.

  1. Darius

    Darius Guest

    Hi,
    How can I order the entries in a hash if I use a reference to a hash
    instead of a hash. For e.g.

    $ref={
    key=>"value",
    };

    instead of

    %ref=(
    key=>"value",
    );

    Thanks!
     
    Darius, Feb 11, 2004
    #1
    1. Advertising

  2. Darius

    Ben Morrow Guest

    (Darius) wrote:
    > Hi,
    > How can I order the entries in a hash if I use a reference to a hash
    > instead of a hash. For e.g.
    >
    > $ref={
    > key=>"value",
    > };
    >
    > instead of
    >
    > %ref=(
    > key=>"value",
    > );


    I'm not quite sure what you mean... is the answer you're looking for
    sort keys %$hash
    ? If not, tell us how you'd do waht yu want with a hash, and we'll
    tell you how to do it with a hashref :).

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
     
    Ben Morrow, Feb 11, 2004
    #2
    1. Advertising

  3. [posted & mailed]

    On 11 Feb 2004, Darius wrote:

    >How can I order the entries in a hash if I use a reference to a hash
    >instead of a hash. For e.g.


    Tie a hash, using the hash reference.

    use Tie::IxHash;

    my $ref = {};
    tie %$ref, 'Tie::IxHash';

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Feb 11, 2004
    #3
  4. Ben Morrow wrote:
    > I'm not quite sure what you mean... is the answer you're looking
    > for
    > sort keys %$hash
    > ? If not, tell us how you'd do waht yu want with a hash, and we'll
    > tell you how to do it with a hashref :).


    The subject line indicates that OP wonders if Tie::IxHash (preserving
    the original order, not necessarily a particular sort order) can be
    used without a named hash.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #4
  5. Jeff 'japhy' Pinyan wrote:
    > On 11 Feb 2004, Darius wrote:
    >> How can I order the entries in a hash if I use a reference to a
    >> hash instead of a hash. For e.g.

    >
    > Tie a hash, using the hash reference.
    >
    > use Tie::IxHash;
    >
    > my $ref = {};
    > tie %$ref, 'Tie::IxHash';


    Did you try that, Jeff?

    I did, and found that the original order is not preserved that way.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 11, 2004
    #5
  6. On Wed, 11 Feb 2004, Gunnar Hjalmarsson wrote:

    >Jeff 'japhy' Pinyan wrote:
    >> On 11 Feb 2004, Darius wrote:
    >>> How can I order the entries in a hash if I use a reference to a
    >>> hash instead of a hash. For e.g.

    >>
    >> Tie a hash, using the hash reference.
    >>
    >> use Tie::IxHash;
    >>
    >> my $ref = {};
    >> tie %$ref, 'Tie::IxHash';

    >
    >Did you try that, Jeff?


    Yes.

    >I did, and found that the original order is not preserved that way.


    I did, and found that the original order is preserved that way.

    use Tie::IxHash;

    local ($,) = ("\n", " ");
    my ($r1,$r2);

    tie %$r1, 'Tie::IxHash';

    @$r1{a..z} = @$r2{a..z} = ();

    print for keys %$r1, "\n"; # abcdefghijklmnopqrstuvwxyz
    print for keys %$r2, "\n"; # wraxdjyukhgftienvmslcpbqzo

    Perl 5.8.0 seems to handle the situation just fine.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Feb 11, 2004
    #6
  7. Darius

    Ben Morrow Guest

    Gunnar Hjalmarsson <> wrote:
    > Ben Morrow wrote:
    > > I'm not quite sure what you mean... is the answer you're looking
    > > for
    > > sort keys %$hash
    > > ? If not, tell us how you'd do waht yu want with a hash, and we'll
    > > tell you how to do it with a hashref :).

    >
    > The subject line indicates that OP wonders if Tie::IxHash (preserving
    > the original order, not necessarily a particular sort order) can be
    > used without a named hash.


    must-learn-to-read-subject-lines... :(

    Ben

    --
    We do not stop playing because we grow old;
    we grow old because we stop playing.
     
    Ben Morrow, Feb 11, 2004
    #7
  8. Jeff 'japhy' Pinyan wrote:
    >
    > use Tie::IxHash;
    >
    > local ($,) = ("\n", " ");
    > my ($r1,$r2);
    >
    > tie %$r1, 'Tie::IxHash';
    >
    > @$r1{a..z} = @$r2{a..z} = ();
    >
    > print for keys %$r1, "\n"; # abcdefghijklmnopqrstuvwxyz
    > print for keys %$r2, "\n"; # wraxdjyukhgftienvmslcpbqzo
    >
    > Perl 5.8.0 seems to handle the situation just fine.


    Hmm.. Please consider the following example:

    use Tie::IxHash;
    my ($r1,$r2,$r3);
    tie %$r1, 'Tie::IxHash';
    tie %$r2, 'Tie::IxHash';
    tie %$r3, 'Tie::IxHash';

    print $r2, "\n"; # HASH(0x158123c)

    %$r1 = ( one => 1, two => 2, three => 3 );
    $r2 = { one => 1, two => 2, three => 3 };

    print $r2, "\n"; # HASH(0x158132c)
    # -----------^^
    $r3->{one} = 1;
    $r3->{two} = 2;
    $r3->{three} = 3;

    print $r1->{$_} for keys %$r1; # 123
    print "\n";
    print $r2->{$_} for keys %$r2; # 312
    print "\n";
    print $r3->{$_} for keys %$r3; # 123
    print "\n";

    I'd appreciate some help to understand what I'm doing. :)
    Is the difference in result because the construct

    $hashref = { };

    *always* creates a *new* anonymous hash, dropping previously created
    referent?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 12, 2004
    #8
  9. On Thu, 12 Feb 2004, Gunnar Hjalmarsson wrote:

    > use Tie::IxHash;
    > tie %$r2, 'Tie::IxHash';
    >
    > print $r2, "\n"; # HASH(0x158123c)
    > $r2 = { one => 1, two => 2, three => 3 };
    > print $r2, "\n"; # HASH(0x158132c)


    >I'd appreciate some help to understand what I'm doing. :)
    >Is the difference in result because the construct
    >
    > $hashref = { };
    >
    >*always* creates a *new* anonymous hash, dropping previously created
    >referent?


    Right. The hash that is tied is no longer accessible via %$r2, because
    $r2 refers to a different hash reference.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Feb 12, 2004
    #9
  10. Gunnar Hjalmarsson wrote:
    >
    > use Tie::IxHash;
    > tie %$r2, 'Tie::IxHash';
    >
    > print $r2, "\n"; # HASH(0x158123c)
    >
    > $r2 = { one => 1, two => 2, three => 3 };
    >
    > print $r2, "\n"; # HASH(0x158132c)
    > # -----------^^
    >
    > Is the difference in result because the construct
    >
    > $hashref = { };
    >
    > *always* creates a *new* anonymous hash, dropping previously
    > created referent?


    Thanks, Kevin and Jeff, for helping me realize my mistake. Sometimes
    it takes some time to do so...

    Btw, I think that OP may have made the same mistake that I did:

    Darius wrote:
    > How can I order the entries in a hash if I use a reference to a
    > hash instead of a hash. For e.g.
    >
    > $ref={
    > key=>"value",
    > };
    >
    > instead of
    >
    > %ref=(
    > key=>"value",
    > );


    But now he should have got enough guidance from this thread to solve
    whatever problem he had. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Feb 12, 2004
    #10
  11. Darius

    Darius Guest

    Gunnar Hjalmarsson <> wrote in message news:<c0esmc$16b5iq$-berlin.de>...
    > Jeff 'japhy' Pinyan wrote:
    > >
    > > use Tie::IxHash;
    > >
    > > local ($,) = ("\n", " ");
    > > my ($r1,$r2);
    > >
    > > tie %$r1, 'Tie::IxHash';
    > >
    > > @$r1{a..z} = @$r2{a..z} = ();
    > >
    > > print for keys %$r1, "\n"; # abcdefghijklmnopqrstuvwxyz
    > > print for keys %$r2, "\n"; # wraxdjyukhgftienvmslcpbqzo
    > >
    > > Perl 5.8.0 seems to handle the situation just fine.

    >
    > Hmm.. Please consider the following example:
    >
    > use Tie::IxHash;
    > my ($r1,$r2,$r3);
    > tie %$r1, 'Tie::IxHash';
    > tie %$r2, 'Tie::IxHash';
    > tie %$r3, 'Tie::IxHash';
    >
    > print $r2, "\n"; # HASH(0x158123c)
    >
    > %$r1 = ( one => 1, two => 2, three => 3 );
    > $r2 = { one => 1, two => 2, three => 3 };
    >
    > print $r2, "\n"; # HASH(0x158132c)
    > # -----------^^
    > $r3->{one} = 1;
    > $r3->{two} = 2;
    > $r3->{three} = 3;
    >
    > print $r1->{$_} for keys %$r1; # 123
    > print "\n";
    > print $r2->{$_} for keys %$r2; # 312
    > print "\n";
    > print $r3->{$_} for keys %$r3; # 123
    > print "\n";
    >
    > I'd appreciate some help to understand what I'm doing. :)
    > Is the difference in result because the construct
    >
    > $hashref = { };
    >
    > *always* creates a *new* anonymous hash, dropping previously created
    > referent?



    Hi, Yes I guess so. Therefore instead of using
    $r2 = { one => 1, two => 2, three => 3 };
    We have to use
    %$r2 = ( one => 1, two => 2, three => 3 );
    Then it prints 123.
    Thanks Guys!
     
    Darius, Feb 12, 2004
    #11
    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. pillbug
    Replies:
    2
    Views:
    495
  2. SpecialK

    Tie a file to a hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    1
    Views:
    480
    Ben Morrow
    Feb 19, 2004
  3. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  4. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    475
    botfood
    Apr 26, 2006
  5. Pat

    Tie::IxHash::Easy with "eval()"

    Pat, Feb 6, 2008, in forum: Perl Misc
    Replies:
    1
    Views:
    117
    comp.llang.perl.moderated
    Feb 9, 2008
Loading...

Share This Page