minor update: I forgot to give the necessary admonition about strict and warnings, but I see that dwm042 has covered that below.
Here's some code that does basically what apl is suggesting (in the spirit of getting you started, but leaving some things for you to do to enhance the learning experience):
#! /usr/bin/perl
use strict;
use warnings;
my @file1 = (
'A101 something 123',
'A102 else 456',
'A103 it 789',
'A104 the 012'
);
my @file2 = (
'A102 else 456',
'A103 it 789'
);
my %prod_id;
foreach my $line ( @file1 ) {
my( $num, $name, $id ) = (split /\s+/, $line);
$prod_id{$num} = $id;
}
foreach my $line ( @file2 ) {
my( $num, $name ) = (split /\s+/, $line);
print "$num\t$name\t$prod_id{$num}\n";
}
There is still a lot of work that you need to do with this. For example, to make it easy on myself, I take sample data from arrays, not files (you will need to fix that part). I also took some shortcuts by (1) always ignoring ProductName, (2) always assuming that the relevant ProductID exists as a key in the hash (this really needs to be checked at the point where I use $prod_id{$num} above).
As I said, this is not offered as a complete solution, rather as something to get you started. |