Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
XP is just a number
 
PerlMonks  

Re: hash of hashes

by marinersk (Chaplain)
on Oct 06, 2013 at 03:34 UTC ( #1057105=note: print w/ replies, xml ) Need Help??


in reply to hash of hashes

Okay, so you need:

  1. Some code to read file1;
  2. Some code to store its data into hashes;
  3. Some code to write file2.

Hash of Hashes can be a bit complicated the first time you try them, can even be intimidating.

But surely if you are trying to do Hash of Hashes, you have long since learned how to read and write files, yes?

So, in keeping with How do I post a question effectively?, why don't you show us what you have thus far, and let us guide you through the changes needed to dive into these potentially intimidating (but eventually easy to use) Hash of Hashes?


Comment on Re: hash of hashes
Re^2: hash of hashes
by Anonymous Monk on Oct 06, 2013 at 05:22 UTC
    #!/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.

      $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.

      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://1057105]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-04-19 11:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls