in reply to how to iterate over multiple hashes with same key value
G'day sunil9009,
I strongly suspect poorly named variables are one of your biggest problems here:
Not surprisingly, when you got to printing your results, you had no idea what data was held where!
You probably want something closer to this:
$ perl -Mstrict -Mwarnings -le ' my @file_data = ( "12:10 a america", "12:11 b bombay", "12:12 c calcutta", "12:13 a australia", "2:30 b bhutan", "3:40 n neterland" ); my %name_data; for (@file_data) { my ($time, $name, $loc) = split; push @{$name_data{$name}{time}} => $time; push @{$name_data{$name}{loc}} => $loc; } for my $name (sort keys %name_data) { print "$name: @{$name_data{$name}{time}} : @{$name_data{$name} +{loc}}"; } ' a: 12:10 12:13 : america australia b: 12:11 2:30 : bombay bhutan c: 12:12 : calcutta n: 3:40 : neterland
Other issues:
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to iterate over multiple hashes with same key value
by sunil9009 (Acolyte) on Jul 02, 2013 at 20:01 UTC |