As best as I can understand, you are trying to do the following: check a list of IP addresses (IPs.txt) against a list of IP address ranges (provided in source.txt), and print each address in the list of IP addresses, along with a region if the address matched a range.

Going on this premise, I've rewritten most of your code to do just that. The big problems with your existing code are as follows:

Here is the code, rewritten to comply with the guidelines I listed, and intended to accomplish the goal I guessed you're trying for.

source.txt
123.123.123.123 124.124.124.124 Canada BC 111.111.111.111 112.112.112.112 Canada AB 11.11.11.11 12.12.12.12 USA WA 22.22.22.22 23.23.23.23 USA OR 33.33.33.33 34.34.34.34 USA CA
IPs.txt
10.10.10.10 11.11.12.12 123.123.122.122 123.123.124.125
Perl program
#!perl # use strict ; use warnings ; open INFO, 'IPs.txt' or die $! ; chomp( my @IPs = <INFO> ) ; close INFO ; my @rows = () ; open IN, ' source.txt' or die $! ; while (<IN>) { next if /^$/ ; my %table = () ; @table{ 'ip', 'ip2', 'country', 'region' } = split ; $table{ 'num_ip' } = sprintf '%03d' x 4, split /\./, $table{ 'ip' + } ; $table{ 'num_ip2' } = sprintf '%03d' x 4, split /\./, $table{ 'ip2 +' } ; push @rows, \%table ; } close IN; open OUT, '>>conversion.txt' or die $! ; foreach my $ip ( @IPs ) { my $match = 0 ; my $num_ip = sprintf '%03d' x 4, split /\./, $ip ; foreach my $row ( @rows ) { if ( $num_ip >= $row->{'num_ip'} && $num_ip <= $row->{'num_ip2 +'} ) { printf OUT "%-16s %s\n", $ip, $row->{'region'} ; $match++ ; } } printf OUT "%-16s\n", $ip if !$match ; } close OUT;
Output
10.10.10.10 11.11.12.12 WA 123.123.122.122 123.123.124.125 BC

Let me know how close to the mark this is.


_______________
D a m n D i r t y A p e
Home Node | Email

In reply to Re: comparing list to 2 cells in array by DamnDirtyApe
in thread comparing list to 2 cells in array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.