jonsmith1982 has asked for the wisdom of the Perl Monks concerning the following question:

any ideas on how i can tell if 1 ip is higher than the other?

heres what ive thought might work, but didn't.
#!/usr/bin/perl -w use strict; # ip address limit # 83.1.1.1 my $a_end = 83; my $b_end = 1; my $c_end = 1; my $d_end = 1; # ip address start # 82.1.1.1 my $a_start = 82; my $b_start = 1; my $c_start = 1; my $d_start = 1; my @calc = ($a_start, $b_start, $c_start, $d_start, $a_end, $b_end, $c +_end, $d_end); my $calc; my $calc1; my $calc2; foreach $calc (@calc) { if ($calc<100) { if ($calc<10) { $calc="00".$calc; } else { $calc="0".$calc; } } } $calc1 = $a_start . $b_start . $c_start . $d_start; $calc2 = $a_end . $b_end . $c_end . $d_end; print $calc1." + ".$calc2." = \n"; if ($calc1>=$calc2) { print "error!\n"; exit(0); } exit(0);

Replies are listed 'Best First'.
Re: comparing two ip addresses.
by ikegami (Patriarch) on Sep 15, 2006 at 21:55 UTC
    use Socket qw( inet_aton ); if (inet_aton('83.1.1.1') lt inet_aton('82.1.1.1')) { print("is less than\n"); } else { print("isn't less than\n"); }

    All the string comparison operators (lt, gt, le, ge, eq, ne and cmp) work here.

    This "trick" is also useful for sorting.

    my @sorted_ips = map substr($_, 4), sort map inet_aton($_).$_, @ips;

    You might also find the modules Net::IP or NetAddr::IP of interest.

    By the way, the problem is your code is that you pad the elements of @calc, yet you use $*_start and $*_end (not the padded elements) to form $calc1 and $calc2.

    Update: Added the sorting code and the list of relevant modules.
    Update: Changed substr($_, 5) to substr($_, 4).
    Update: Added the explanation as to why the OP's code didn't work.

      Thanks for pointing that out to me, i would of carried on making that mistake if it wasnt for you and perlmonks.
Re: comparing two ip addresses.
by shmem (Chancellor) on Sep 16, 2006 at 00:22 UTC
    I use pack and unpack for IP adresses:
    my $ip = '83.1.1.1'; # turn to 32 bit integer my $ip_int = unpack "N",pack "C4", split/\./,$ip;
    $ip_int is now 1392574721, and 82.1.1.1 yields 1375797505.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: comparing two ip addresses.
by CountZero (Bishop) on Sep 16, 2006 at 08:57 UTC
    An interesting concept, but I'm not sure that ip-addresses support the idea of "greater than" or "lower than".

    One can check if they are equal or not and whether they belong to the same interval or not, but it is my feeling that saying that an ip address is "greater than" another ip address has no physical reality behind it.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      IP-addresses are numbers, and as such, they can be compared in terms of greater or lesser. And it makes sense. It happens to be that the lowest ip address in a given range is the network address, the highest is the broadcast address. How would you say that avoiding the words low and high, greater and lesser?

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        your statement about IP addresses whilst true in general, is not technically correct. you can actually choose any address you like for the network and broadcast, as long as all the devices on this net know the same.
        also...as the other monks said...ip adresses are not lower or higher, when you are talking about IP addressing...they're either equal or not equal. although we may consider them in those terms (higher/lower) for purposes of sorting.
        the hardest line to type correctly is: stty erase ^H
      I was going to say as much in my post, then I realized there are two reasons to do comparisons: 1) To provide a consistent display order, and 2) To sort the start and end addresses of a range before iterating over said range. The OP is clearly doing the second, based on the names of his variables.

      It's useful for sorting, which is useful for grouping.

      We're building the house of the future together.