[26981] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8926 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 6 18:05:56 2006

Date: Mon, 6 Feb 2006 15:05:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 6 Feb 2006     Volume: 10 Number: 8926

Today's topics:
        Problem sorting multidimentionnal hash on insertion ord <scottalorda_NOSPAM@libello.com>
    Re: Problem sorting multidimentionnal hash on insertion <1usa@llenroc.ude.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Mon, 06 Feb 2006 21:32:27 +0100
From: =?ISO-8859-1?Q?S=E9bastien_Cottalorda?= <scottalorda_NOSPAM@libello.com>
Subject: Problem sorting multidimentionnal hash on insertion order
Message-Id: <43e7b28f$0$17074$626a54ce@news.free.fr>

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


------------------------------

Date: Mon, 06 Feb 2006 20:46:03 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Problem sorting multidimentionnal hash on insertion order
Message-Id: <Xns9762A07B26612asu1cornelledu@127.0.0.1>

Sébastien Cottalorda <scottalorda_NOSPAM@libello.com> wrote in
news:43e7b28f$0$17074$626a54ce@news.free.fr: 

> #!/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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8926
***************************************


home help back first fref pref prev next nref lref last post