C:\Steve\Dev\PerlMonks\P-2013-10-06@2342-Hash-Of-Hashes>FruitColor1b.pl
strawberry red green rose
red
Added hash{red}{strawberry}
green
Added hash{green}{strawberry}
rose
Added hash{rose}{strawberry}
apple red rose
red
Added hash{red}{apple}
rose
Added hash{rose}{apple}
mango green
green
Added hash{green}{mango}
-----[ Results ]---------------
key = 'rose' subkey = 'strawberry'
key = 'rose' subkey = 'apple'
key = 'green' subkey = 'mango'
key = 'green' subkey = 'strawberry'
key = 'red' subkey = 'strawberry'
key = 'red' subkey = 'apple'
####
use strict;
use warnings;
##
##
my %hash = ();
##
##
for ($i = 1;$i <= $#line;$i++)
{
$d=$line[0];
print "$d";
# $d[$k++]=$line[$i],1;
$hash{$d}{$i}=$line[$i];
print "$hash{$d}{$i}\n";
}
##
##
{apple}{1} = 'red'
{apple}{2} = 'green'
##
##
{apple}{red}
{apple}{green}
##
##
foreach my $color (@colors) # Was: for ($i = 1;$i <= $#line;$i++)
{
$hash{$color}{$fruit} = 1; # Was: $hash{$d}{$i}=$line[$i];
print "Added hash{$color}{$fruit}\n"; # Was: print "$hash{$d}{$i}\n";
}
##
##
}
close (MYFILE);
##
##
foreach my $key (keys %hash)
{
# -----[ Here is how you read a Hash of Hashes ]---------------
foreach my $subkey (keys %{$hash{$key}}) # Was: my $array = $hash{$key};
{
print "key = '$key' subkey = '$subkey'\n";
}
}