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 reply to Re: Re: Re: Flat File Question by hmerrill
in thread Flat File Question by lisaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.