in reply to Re^2: hash of hashes
in thread hash of hashes
Since this looks like homework, I want you to understand what needs to be fixed, not just hand you working code and leave you lost when your next assignment builds on this one.
I made the changes discussed under the "Read more" link below. Then I adjusted some of the print statements so they had newlines and indentation differently than you had been doing.
This is what happened when I did that to your script:
C:\Steve\Dev\PerlMonks\P-2013-10-06@2342-Hash-Of-Hashes>FruitColor1b.p +l 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'
Do I have your attention now? :-). Then let's get to it:
And since you are using strict now, you will need to pre-declare your hash:use strict; use warnings;
Chafe not -- this is a good thing. Please just trust me on this.my %hash = ();
@line = split /[\s]+/;
Nice use of split. Let's fix a few things.
So try this instead:
my ($fruit, @colors) = split /\s+/;
This way we pull out the fruit and the array of colors in one fell swoop.
Admit it -- Perl is kinda' neat, isn't it? :-)
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"; }
What you almost ended up with is something like:
What you wanted was more like:{apple}{1} = 'red' {apple}{2} = 'green'
{apple}{red} {apple}{green}
The '1' doesn't really mean anything. We just need to put a value into that slot in the Hash of Hashes. That way, later, when we look through the keys of the hash, there's something there to find.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"; }
Ironically, the value isn't what we're after. It's the organization of the keys.
More Perl goodness. :-)
You had this at the bottom of your code:
Move it up so it is right after the foreach loop we just discussed.} close (MYFILE);
This one I'm just going to hand to you, since the syntax isn't particularly intuitive at first. But learn this stuff -- most notably, you should really disassemble the inner foreach loop until you understand it. You won't believe how often it will help you take easy shortcuts in Perl later.
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"; } }
Holler with any questions. The more you understand this, the more of Perl's innate power you are going to be able to unleash at will.
|
|---|