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)
|