Hashes are (by definition) unordered so when you call values you're getting the values in a (pseudo)random order. If you want them back in the order of the keys you either need to store the keys off into an array as they come in, or use keys on your hash (which will return items in the same order as the corresponding values; alternately sort the keys and iterate over that to pull out and print the corresponding value).
Update: Just to expand merging the suggestion above roughly something like this.
my %hash;
my @key_order;
while( defined( my $line = <> ) ) {
my ($first, $third) = (split ' ', $line)[0, 2];
$hash{ $first } = $third;
push @key_order, $first;
}
## In the order they appeared . . .
for my $key (@key_order) {
say $hash{ $key };
}
## Or in their random ordering but with the match . . .
for my $key ( keys %hash ) {
say qq{$key => $hash{ $key }};
}
The cake is a lie.
The cake is a lie.
The cake is a lie.
|