Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: Compare Arrays, Return Values

by broomduster (Priest)
on Aug 28, 2008 at 15:38 UTC ( [id://707521]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://707521]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found