1. loop through file 2 lines, and create a hash for
each line where the key is the company name, and
the value is the whole pipe delimited record.
2. loop through file 1 lines - for each file 1 line,
check for a match in the file 2 hash.
#!/usr/bin/perl -w
use strict;
my ($company, $url, $directory);
### 1. loop through file 2, building a hash where the
### key = company name
### value = whole pipe delimited record
my $file2="/path/to/company_file2.dat";
open(FILE2, "<$file2") || die "Can't open $file2: $!";
my %file2_hash = ();
while (<FILE2>) {
my $line = chomp($_);
($company, $url, $dir) = split(/\|/, $line);
### This next statement creates entries in hash
### %file2_hash.
### - the key in that hash is $company
### - the value in that hash is a *reference*
### to an anonymous hash - the curly braces on the
### right of the equals sign create a reference
### to an anonymous hash. The hash that the
### reference points to has 2 keys - one key is
### "url", and the other key is "dir".
$file2_hash{$company} = { "url" => $url,
"dir" => $dir };
} ### end while (<FILE2>)
close(FILE2);
### 2. loop through file 1, and for each line in file 1,
### see if there's a matching key(company name) in
### the file2 hash
my $file1="/path/to/company_file.dat";
open(FILE1, "<$file1") || die "Can't open $file1: $!";
print "Content-type: text/html\n\n";
print "Company Listings:<BR>";
while (<FILE1>) {
my $line = chomp($_);
($file1_company, $file1_url, $file1_dir) = split(/\|/, $line);
print "$file1_company, $file1_url, $file1_dir<BR>\n";
if (exists($file2_hash{$company}) {
### found a match ###
print qq!$file1_company, $file2_hash{$company}{"url"}, $file2
+_hash{$company}{"dir"}<BR>!;
}
} ### end while (<FILE1>)
close(FILE1);
Careful, as this code is completely untested - but hopefully it would work without major changes.
HTH. |