http://www.perlmonks.org?node_id=1057105


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?

Replies are listed 'Best First'.
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.