Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: hash of hashes

by Anonymous Monk
on Oct 06, 2013 at 05:22 UTC ( #1057118=note: print w/replies, xml ) Need Help??

in reply to Re: hash of hashes
in thread hash of hashes

#!/usr/local/bin/perl open (MYFILE, 'test1.txt'); while (<MYFILE>) { chomp; print "$_\n"; @line = split /[\s]+/; 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"; } for my $key (keys %hash) { my $array = $hash{$key}; print "$_ $key, " ; # print $hash{$key}; # print "$_ $key, "; # foreach $x(keys %hash) # { # print $hash{$x} ; # } } } close (MYFILE);

i am not an expert but i tried like this my logic not correct for handling hash of hashes.

Replies are listed 'Best First'.
Re^3: hash of hashes
by AnomalousMonk (Chancellor) on Oct 06, 2013 at 05:39 UTC

    At this point in the code,  $d is a fruit,  $i is an index (a number), and  $line[$i] is a color (AFAIU your code). Why would you want to associate a number with a fruit and a color with a number (perhaps better expressed as) a fruit with a number and a number with a color? Please take a look at Re^3: hash of hashes.

    Also, try using Data::Dumper or Data::Dump (my favorite) for looking at data structures — much more convenient.

    And again, please take a look at perldsc.

Re^3: hash of hashes
by marinersk (Priest) on Oct 07, 2013 at 07:09 UTC
    Thank you for showing your code -- I see where you lost your way. In the "read more" link below, I'll cover all the stuff you need to fix.

    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:

    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057118]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-02-23 05:32 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.