in reply to Flat File Question

Two options I can think of:
1. if the two files are relatively small, you can read them both into hashes, where the key is the company name (assuming the company name is what you want to match on) and the value is the whole pipe delimited record. Loop through the 1st hash, like you did in the "for" above, and for each hash element, check for a matching company name in the other hash. This would be the fastest (performance) approach. 2. loop through the records in file 2 - create a hash where the key is the company name, and the value is the whole pipe delimited record. After that loop is done and the hash for file 2 has been created, loop through file 1 records - for each record, see if there is a matching company name in the file 2 hash.
If the files are too big, the last resort might be to have an outer loop to loop through the file 1 records, and an inner loop that for each file 1 record, will loop through all the records in file 2, looking for a company name match.

HTH.

Replies are listed 'Best First'.
Re: Re: Flat File Question
by lisaw (Beadle) on Oct 24, 2003 at 23:48 UTC
    Hi HMerrill, Is there any way that you could provide an example of the first option? I'm still learning :) Thank you, lis

      I'm still learning too :-)

      Here's some code to

      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.

        In the 2nd loop, the "if" is incorrect - this:
        if (exists($file2_hash{$company}) { ### found a match ### print qq!$file1_company, $file2_hash{$company}{"url"}, $file2_ha +sh{$company}{"dir"}<BR>!; }
        should be changed to this:
        if (exists($file2_hash{$file1_company}) { ### found a match ### print qq!$file1_company, $file2_hash{$file1_company}{"url"}, $fi +le2_hash{$file1_company}{"dir"}<BR>!; }