Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using Tie::IxHash order a hash reference

Reply
Thread Tools

Using Tie::IxHash order a hash reference

 
 
Darius
Guest
Posts: n/a
 
      02-11-2004
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!
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      02-11-2004

http://www.velocityreviews.com/forums/(E-Mail Removed) (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] (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      02-11-2004
[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)

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-11-2004
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

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-11-2004
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

 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      02-11-2004
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)

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-11-2004

Gunnar Hjalmarsson <(E-Mail Removed)> 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.
(E-Mail Removed)
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-12-2004
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

 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      02-12-2004
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)

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-12-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)
> # -----------^^
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
Hash#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM
hash reference as a hash key Steve Perl Misc 6 09-28-2003 10:49 AM



Advertisments