Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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.


Comment on Re^2: hash of hashes
Download Code
Re^3: hash of hashes
by AnomalousMonk (Abbot) on Oct 06, 2013 at 05:39 UTC
    $hash{$d}{$i}=$line[$i];

    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 (Chaplain) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1057118]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (12)
As of 2014-12-18 21:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (66 votes), past polls