in reply to Re^2: Compare Arrays, Return Values
in thread Compare Arrays, Return Values

I don't mean to be rude, but you've just described the problem. How can you be at a loss as to what to do?

Open file 1
Open file 2

Replies are listed 'Best First'.
Re^4: Compare Arrays, Return Values
by de2425 (Sexton) on Aug 28, 2008 at 15:25 UTC
    I understand what you're saying but I still am unsure as to how the appropriate "UniversalID" values will be written along with the "ProductID" values. That is where my confusion is coming in.
      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.