in reply to Re: ip sorting
in thread ip sorting

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

Replies are listed 'Best First'.
Re: Re: Re: ip sorting
by Anonymous Monk on Mar 24, 2003 at 00:06 UTC
    I had asked the original question regarding ip sorting. I
    had to come back in here because I'd said that the man -g
    worked fine. However, the list I'd tested it on wasn't
    long enough for a real test. It does not work.

    However pg's slight modification to Zaxo's original code
    did the trick. Thanks very much.

    My apologies for duplicating a thread. I've not been here before.

    Thanks everyone!