Using Tie::IxHash order a hash reference

D

Darius

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!
 
B

Ben Morrow

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
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

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';
 
G

Gunnar Hjalmarsson

Ben said:
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.
 
G

Gunnar Hjalmarsson

Jeff said:
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.
 
J

Jeff 'japhy' Pinyan

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.
 
B

Ben Morrow

Gunnar Hjalmarsson said:
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
 
G

Gunnar Hjalmarsson

Jeff said:
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?
 
J

Jeff 'japhy' Pinyan

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.
 
G

Gunnar Hjalmarsson

Gunnar said:
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:
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. :)
 
D

Darius

Gunnar Hjalmarsson said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top