Re: Hashes of Hashes
- From: jen.spinney@xxxxxxxxx (Jen Spinney)
- Date: Fri, 29 Dec 2006 11:34:59 -0500
On 12/28/06, Jason Malburg <jcmalburg@xxxxxxxxx> wrote:
Hi,
I'm working on a complex data structure with hashes of hashes and
hashes of arrays and I noticed a weird behavior I was hoping someone
could explain. Here's a sample:
$Data{US17}{1}{GROUP} = "STRING";
$Data{US17}{1}{GROUP}{.001} = 5;
$Data{US17}{1}{GROUP}{.002} = 6;
$Data{US17}{1}{GROUP}{.003} = 7;
print "group = $Data{US17}{1}{GROUP}\n";
for my $time (keys %{$Data{US17}{1}{GROUP}}){
print "time = $time $Data{US17}{1}{GROUP}{$time}\n";
}
The output is:
group = STRING
time = 0.002 6
time = 0.003 7
time = 0.001 5
If I swap the order of the declarations, then all of the keys for the
hash under 'GROUP' are wiped out. For example:
$Data{US17}{1}{GROUP}{.001} = 5;
$Data{US17}{1}{GROUP}{.002} = 6;
$Data{US17}{1}{GROUP}{.003} = 7;
$Data{US17}{1}{GROUP} = "STRING";
only produces:
group = STRING
Is this expected behavior? Am I getting lucky in the first instance
with bad syntax that just happens to do what I want?
Thanks in advance,
Jason
Hi Jason,
If you want to make hashes of hashes or any other complex data
structure like that, you probably want to be using references. Perl
flattens arrays/hashes contained in another array/hash. That is, (1,
2, 3, 4, (1, 2, 3)) is the same as (1, 2, 3, 4, 1, 2, 3). Take a look
at perldoc perldsc and/or perldoc perlreftut. You might also want to
use the Data::Dumper module to take see what your data structures
actually look like (so you know you've done it correctly). If you add
this to your code:
use Data::Dumper;
....
print Dumper(%Data);
You'll probably see that your data structure isn't what you intended.
Hope that helps.
- Jen
.
- Follow-Ups:
- Re: Hashes of Hashes
- From: Uri Guttman
- Re: Hashes of Hashes
- References:
- Hashes of Hashes
- From: Jason Malburg
- Hashes of Hashes
- Prev by Date: Re: Deleting files remotely
- Next by Date: Re: Hashes of Hashes
- Previous by thread: Hashes of Hashes
- Next by thread: Re: Hashes of Hashes
- Index(es):
Relevant Pages
|