in reply to Comparing 1 list and 1 array

Let me start with the most obvious issues: I would be inclined to use a hash to store the table of IP addresses and countries. You'd construct the hash something like this:
my %table; open(IN, '<Source.txt') or die; while (<IN>) { chomp; my($ip, $country) = split(' ', $_, 2); $table{$ip} = $country; }
You'll have to make sure that split() call correctly splits your input data into IP and country name, of course.

Once you've done that--and assuming you've removed newlines from your input data--you can do country lookups like this:

open(OUT, '>>conversion.txt') or die; foreach $line1 (@lines1) { if (exists $table{$line1}) { print OUT $line1, ' ', $table{$line1}, "\n"; } } close OUT;

Replies are listed 'Best First'.
Re: Re: Comparing 1 list and 1 array
by Anonymous Monk on Jun 25, 2002 at 17:50 UTC
    Thanks everyone for your help. I had wondered about using a %file, but I wanted a 2 dimentional array, so figured that would be how to get one. :/ I have taken out reading into $file, and opened the list straight into INFO, and gone with a hash table. My teammates asked that I print the IP if I couldn't find it in the table, so I added an else. I'm still working on the hash table, to make sure I don't need more than those 2 fields. Its been over 10 years since I did any Perl scripting, so I am WAY out of practice! Here is the second interation of code: #!/perl # open(INFO, '<IPs.txt' ) or die; @lines1 = <INFO> ; close(INFO) ; my %table open(IN, '<source.txt' ) or die; while (<IN>) { chomp; my($ip, $country) = split(' ',$_,2); $table($ip) = $country; } open(OUT, ">>conversion.txt") or die; foreach $line1 (@lines1) { if (exists $table($line1)) { if $line2 =~ /$line1/ ; print OUT $line1, ' ',$table($line1),"\n" ; else else print OUT $line1; } } close OUT;