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.


In reply to Re: comparing two ip addresses. by ikegami
in thread comparing two ip addresses. by jonsmith1982

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.