On 6/2/07, Andrej Kastrin <[EMAIL PROTECTED]> wrote:
Deal all,

if the key already exists in the hash, then its value is overwritten.
So, if I have the following structure of the input file

A foo
A faa
A hoo
B foo
B aaa
C bbb

what is the procedure of choice to store all key-value pairs into the
hash and print it out?

Thanks in advance for any suggestion.

Best, Andrej

It sounds like you are looking for what is called a HoA (hash of
arrays).  it is  a complex data structure built on top of a hash using
array references.  All of that sounds more complicated than it is.
What follows is the relevant section of perldoc perldsc.  You will
probably want to read the whole doc since it contains lots of good
info.  If you have any questions, please ask again.

HASHES OF ARRAYS
      Declaration of a HASH OF ARRAYS

       %HoA = (
              flintstones        => [ "fred", "barney" ],
              jetsons            => [ "george", "jane", "elroy" ],
              simpsons           => [ "homer", "marge", "bart" ],
            );

      Generation of a HASH OF ARRAYS

       # reading from file
       # flintstones: fred barney wilma dino
       while ( <> ) {
           next unless s/^(.*?):\s*//;
           $HoA{$1} = [ split ];
       }

       # reading from file; more temps
       # flintstones: fred barney wilma dino
       while ( $line = <> ) {
           ($who, $rest) = split /:\s*/, $line, 2;
           @fields = split ' ', $rest;
           $HoA{$who} = [ @fields ];
       }

       # calling a function that returns a list
       for $group ( "simpsons", "jetsons", "flintstones" ) {
           $HoA{$group} = [ get_family($group) ];
       }

       # likewise, but using temps
       for $group ( "simpsons", "jetsons", "flintstones" ) {
           @members = get_family($group);
           $HoA{$group} = [ @members ];
       }

       # append new members to an existing family
       push @{ $HoA{"flintstones"} }, "wilma", "betty";

      Access and Printing of a HASH OF ARRAYS


       # one element
       $HoA{flintstones}[0] = "Fred";

       # another element
       $HoA{simpsons}[1] =~ s/(\w)/\u$1/;

       # print the whole thing
       foreach $family ( keys %HoA ) {
           print "$family: @{ $HoA{$family} }\n"
       }

       # print the whole thing with indices
       foreach $family ( keys %HoA ) {
           print "family: ";
           foreach $i ( 0 .. $#{ $HoA{$family} } ) {
               print " $i = $HoA{$family}[$i]";
           }
           print "\n";
       }

       # print the whole thing sorted by number of members
       foreach $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) {
           print "$family: @{ $HoA{$family} }\n"
       }

       # print the whole thing sorted by number of members and name
       foreach $family ( sort {
                                  @{$HoA{$b}} <=> @{$HoA{$a}}
                                              ||
                                          $a cmp $b
                  } keys %HoA )
       {
           print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n";
       }

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to