A couple of points:
Don’t use first as the name of both the hash and the scalar! Yes, Perl can easily disambiguate them, but why make things harder (for humans) to read and comprehend than they need to be?
The line:
$first{$first} = [] if (!$first{$first});
isn’t needed — autovivification will take care of that for you automatically.
#! perl use strict; use warnings; my %names; while (<DATA>) { my ($first, $last) = split; push @{$names{$first}}, $last if $last; } my $name_to_find = 'John'; print "$name_to_find --> ", join(', ', @{$names{$name_to_find}}), "\n" +; __DATA__ John Marry John Lea John Paul David Patrik David Sam
Output:
16:53 >perl 545_SoPW.pl John --> Marry, Lea, Paul 16:56 >
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^2: Accessing hash values in a HOH
by Athanasius
in thread Accessing hash values in a HOH
by modi2020
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |