Given that you have reasons to hold your people in a list then a quick way to search for particular first names is to index them as other replies have suggested. You can build the index as you build the @PEOPLE list and if you make the index a HoL with key being name and value being record no. in @PEOPLE you can extract all of your Johns using a slice.
#!/usr/local/bin/perl -w
#
use strict;
our @PEOPLE = ();
our $rhFirstNameIdx = {};
our $recCt = 0;
while(<DATA>)
{
chomp;
my ($fname, $lname) = split;
push @PEOPLE, {fname => $fname, lname => $lname};
push @{$rhFirstNameIdx->{$fname}}, $recCt ++;
}
our $wanted = 'John';
our @found = @PEOPLE[@{$rhFirstNameIdx->{$wanted}}];
print "First - $_->{fname}, Last - $_->{lname}\n" for @found;
__END__
Fred Bloggs
Charley Farley
John Doe
Peter Piper
Mary Poppins
John Smith
Bill Bailey
The little script above illustrates this and prints out the entries for John Doe and John Smith.
Cheers JohnGG |