I had an idea to write a script that could open a file
of names, first and last, that would search for the most frequently occurring last names and write out the first and last names of all those that match to a new file. I am a beginner, and of course I have quickly
run up against the limit of my knowledge.
Here's what I've got, (not much), hope it makes some sort of sense
names.txt format: -------------- John Smith Sue Jones Dave Smith Jim Beam Frank Sinatra -------------
#!/usr/bin/perl
$infile = "names.txt";
$result_file = "most_common.txt";
open (FILE, "$infile") or die, "can't open $infile: $!\n";
open (OUT, ">$result_file") or die, "can't open $result_file: $!\n";
while (<FILE>) {
#this is wrong, i know how do i split the first and last names apart?
my $first_names = $_ split /W*/;
# how do i get the remainder from the above split into the last names # array bellow
my @last_names =
# Here I'm lost
# My guess is that I need to split first and last apart,
# into a first separate first and last arrays
# then creat a hash table of first and last names, and compare the last # names array to the hash table
# then somehow match the last names that match and print them out
# I don't know how to sort the contents of infile into a hash or compare # them to the contents of a last names array, or even how to split
# first and last into arrays, beyond a vague idea of splitting them on
# white space, with the split function and a regular expression
# That's as far as I've got, any suggestions, tips, etc much appreciated
my %first_last ( 'first' => 'last');
foreach (@last_names =~ %first_last) {
# i know the above is wrong but don't know # how to sort through the hash for matches # or even how to populate the hash
print OUT;
}
}
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>