Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hash of hash of arrays

    Hi!

    I have a problem in printing the following structure:

    $estructura->{$nom}{reaccio}=[$reac];
    $estructura->{$nom}{reactius}=[@reactius];
    $estructura->{$nom}{productes}=[@productes];

    What would be the easiest way to print each element of @reactius and @productes?

    Many thanks!!

  • #2
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    Code:
    $estructura->{$nom}{reaccio} = [$reac];
    $estructura->{$nom}{reactius} = [@reactius];
    $estructura->{$nom}{productes} = [@productes];
    
    for my $key (qw(reactius productes)) {
    	print "$key:\n";
    	for my $val (@{$estructura->{$nom}{$key}}) {
    		print "\t$val\n";
    	}
    }

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Miller but actually it doesn't work.
    The second "for" is printed blank. How can I solve this second step?

    Thanks!

  • #4
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    I advise you to have use strict; and use warnings; turned on. This is always important, but even more so when working with complex data structures.

    There is nothing wrong with the code I gave you given your original information. I therefore advise you to use Data-Dumper to debug your problem.

    Code:
    use Data::Dumper;
    
    use strict;
    use warnings;
    
    ...
    
    $estructura->{$nom}{reaccio} = [$reac];
    $estructura->{$nom}{reactius} = [@reactius];
    $estructura->{$nom}{productes} = [@productes];
    
    for my $key (qw(reactius productes)) {
    	print "$key:\n";
    	print Dumper($estructura->{$nom}{$key});
    }


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •