Andrej Kastrin 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.

Does this do what you want?

use strict;
use warnings;

my %data;

while (<DATA>) {
 my ($key, $val) = split;
 push @{$data{$key}}, $val;
}

foreach my $key (sort keys %data) {

 foreach my $val (@{$data{$key}}) {
   printf "%s %s\n", $key, $val;
 }
}

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


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


Reply via email to