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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|