This solution is actually on the right track.
The only mistake is that the author thought the result from inet_aton is a number, but it is not. In fact, the result returned is a string.
Simply modify that sort statement to:
sort {$a cmp $b}
Now this piece of code works properly (I tested the modified version).
If you are interested in details, inet_aton converts ascii address into a c structure in_addr, i.e.
struct in_addr {
union {
struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
struct { u_short s_w1,s_w2; } S_un_w;
u_long S_addr;
} S_un;
}
,which Perl interprets as a string, not an unsigned long.
My testing code:
use strict;
use Socket qw( inet_aton inet_ntoa);
local $, = $/;
print map { inet_ntoa($_) }
sort { $a cmp $b }
map { chomp; inet_aton($_) }
<DATA>;
print $/;
__DATA__
128.1.1.0
127
23.4.5.6
255.255.255.255
45.27.128.0
localhost
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.