On 12/28/06, Jason Malburg <[EMAIL PROTECTED]> 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
