Velocity Reviews > Perl > Problem sorting multidimentionnal hash on insertion order

# Problem sorting multidimentionnal hash on insertion order

Sébastien Cottalorda
Guest
Posts: n/a

 02-06-2006
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 ?

Sébastien

usenet@DavidFilmer.com
Guest
Posts: n/a

 02-06-2006
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

Paul Lalli
Guest
Posts: n/a

 02-06-2006
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

usenet@DavidFilmer.com
Guest
Posts: n/a

 02-06-2006
(E-Mail Removed) 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';

A. Sinan Unur
Guest
Posts: n/a

 02-06-2006
Sébastien Cottalorda <(E-Mail Removed)> wrote in
news:43e7b28f\$0\$17074\$(E-Mail Removed):

> #!/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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:

xhoster@gmail.com
Guest
Posts: n/a

 02-07-2006
Sébastien_Cottalorda <(E-Mail Removed)> 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

--
Usenet Newsgroup Service \$9.95/Month 30GB

Sébastien Cottalorda
Guest
Posts: n/a

 02-07-2006
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

Cheers.

Sebastien