The standard technique for looking stuff up is to use a hash:

use strict; use warnings; my $file1 = <<FILE1; parcel# 12345 doc num 123 doc num 456 doc num 789 parcel# 67890 doc num 342 doc num 657 doc num 876 FILE1 my $file2 = <<FILE2; doc num 342 data data data data data data data data doc num 657 data data data data data data data data doc num 876 data data data data data data data data doc num 123 data data data data data data data data doc num 456 data data data data data data data data doc num 789 data data data data data data data data FILE2 my %docs; my $currParcel; open my $f1In, '<', \$file1; while (<$f1In>) { chomp; next if ! $_; if (/parcel#\s+(\d+)/) { $currParcel = $1; next; } next if ! defined $currParcel || ! /^doc num (\d+)/; $docs{$1} = $currParcel; } close $f1In; open my $f2In, '<', \$file2; while (<$f2In>) { chomp; next if ! /doc num\s+(\d+)\s+(.*)/; if (! exists $docs{$1}) { warn "Parcel not known for $1\n"; next; } print "parcel# $docs{$1} doc num $1 $2\n"; } close $f2In;

Prints:

parcel# 67890 doc num 342 data data data data data data data data parcel# 67890 doc num 657 data data data data data data data data parcel# 67890 doc num 876 data data data data data data data data parcel# 12345 doc num 123 data data data data data data data data parcel# 12345 doc num 456 data data data data data data data data parcel# 12345 doc num 789 data data data data data data data data

However this task looks like it should really be using a database. If there are more than a few hundred entries in the files and the data is likely to be referenced more than a small number of times a database will make your life much happier (eventually).

True laziness is hard work

In reply to Re: Matching and combining two text files by GrandFather
in thread Matching and combining two text files by koolgirl

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.