Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem sorting multidimentionnal hash on insertion order

Reply
Thread Tools

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 ?

Thanks in advance.

Sébastien
 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
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';

 
Reply With Quote
 
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:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
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


Thanks for your help.

Cheers.

Sebastien

 
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 insertion order preservation in ruby1.9 Joachim (München) Ruby 4 04-10-2008 11:00 AM
Equivalent of Java linked hash map (insertion order) Mosfet C++ 6 01-15-2008 02:09 PM
Is there a hash-like class that maintains insertion order Bob Hutchison Ruby 8 09-28-2005 03:14 AM
[Nuby] Sorting a Hash and keepint it as a Hash? Williams, Chris Ruby 3 12-13-2004 09:04 PM



Advertisments