Problem sorting multidimentionnal hash on insertion order

  • Thread starter Sébastien Cottalorda
  • Start date

S

Sébastien Cottalorda

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
 
Ad

Advertisements

U

usenet

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

Try

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

Paul Lalli

Sébastien Cottalorda said:
#!/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.

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
 
A

A. Sinan Unur

#!/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
 
X

xhoster

Sébastien_Cottalorda said:
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
 
Ad

Advertisements

S

Sébastien Cottalorda

Paul Lalli a écrit :

syntax error.
Right!




wrong variable.
Right!



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

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

Top