in reply to Array in a Hash

I suspect you may be trying to make things more difficult than they need to be. References can be tricky, but once you get the hang of them they make so many other things MUCH easier.

I tried to follow the logic of your example, but I'm afraid I didn't understand the rationale for doing things they way you did. For example, you are manipulating an array inside a hash of hashes (HoH), but you never make use of either level of hash. I assume the example may be a simplified version of the real problem, so I retained that structure in my example below. If there isn't any reason to keep the HoH structure, then simply using the array would be easier.

I rewrote your example (below). You'll notice that I added a few things. First and foremost, strict and warnings are your friends. Use them, especially when you're trying to learn new syntax. There are many problems with your example code that would have been brought to your attention had you used them. Second, a module like Data::Dumper comes in handy when trying to understand complex data structures.

use strict; use warnings; use Data::Dumper; my @fruit = qw( Apple pear Bananna Peach ); my $new_fruit = "Grapes"; my %fruits; $fruits{0}{fruit} = \@fruit; print "Original fruits:\n", Dumper( \%fruits ); push( @{ $fruits{0}{fruit} }, $new_fruit ); print "New fruits:\n", Dumper( \%fruits ); print "There are ", scalar @{ $fruits{0}{fruit} }, " pieces of fruit:\ +n"; foreach my $f ( @{ $fruits{0}{fruit} } ) { print "$f\n"; }

I left the code uncommented intentionally. Try to follow what is going on, but if you need help feel free to ask.

There are lots of good resources to learn about references and visualizing data structures. If I know planetscape, she will post an extensive list. :-)

HTH

Update: In case planetscape is off hunting knights, start with perlref, perlreftut, perldsc, perllol, PerlMonks tutorials about references, and How can I visualize my complex data structure?.

Replies are listed 'Best First'.
Re^2: Array in a Hash
by g_speran (Beadle) on Sep 04, 2009 at 04:42 UTC
    Hello,
    Thank you for your reply. For simplicity sake, at any one given time there can be 21 fruit baskets. Each basket will contain X number of fruit consisting of "Fruit Names". So I was trying to use the hash of 0-20, for a total of 21, ( ex. $fruit{0}=248 to identify the total number of pieces of fruit in the basket, and then use ex. $fruit{0}{'fruits'}, to contain an array of the types of fruit that make up the value of $fruit{0}.

    I see you change "There are $fruit{0} ... "to There are ", scalar @{ $fruits{0}{fruit} }, ", but I actually was wanting to use $fruit{0} to list the total pieces of fruit in the basket. So in this example, Basket 1 contain 248 pieces of fruit consisting of Apples, Pears, Bananas, Peaches, and Grapes

    Thanks
    Gary
      Here is some code that is structured based on your description.

      Coding this way does require some knowledge of perl references. Hopefully, this will show you their power and relative simplicity.

      #!/usr/bin/perl use strict; use warnings; my @basket = ( #Left bracket indicates start of a list ... (Array of h +ash-refs) # Populate Zero'th basket as a hash-ref: { count=> 254, # Expecting this many fruit here fruit=> [ qw( Apple pear Banana Peach ) ], type => "All Season" }, # End of Zero'th basket {} # First (Empty) basket is created (This is not necessary) ); # End of all baskets # Adding "Grapes" to zero'th basket ... my $fruit_ref = $basket[0]{fruit} ; push @$fruit_ref, "Grapes"; $basket[0]{count}++; # Increment how many fruit # Populate First basket with tropical fruit: $basket[1]{fruit} = [ qw|papaya guava mango| ]; $basket[1]{count} = scalar ( @{ $basket[1]{fruit} }); # Auto count $basket[1]{type} = "Tropical"; # Print fruit info.. print "Basket# Type \t\tCount \tContents -------\n"; my $basket_nbr = 0; for my $b (@basket){ $basket_nbr++; #For printing, basket numbers start with 1 print "$basket_nbr \t$b->{type} \t$b->{count} \t@{$b->{fruit} }\n +"; }
      Output:
      Basket# Type Count Contents ------- 1 All Season 255 Apple pear Banana Peach Grapes 2 Tropical 3 papaya guava mango

           Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)