Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How to sort IP addresses

by AltBlue (Chaplain)
on Jan 29, 2007 at 23:07 UTC ( [id://597216]=note: print w/replies, xml ) Need Help??


in reply to How to sort IP addresses

TIMTOWTDI ;-)
$ perl -le 'map { printf "%vd\n", $_ } sort map { eval "v$_" } @ARGV' +\ 1.2.1.1 1.1.1.1 1.11.1.1 1.1.1.66 1.1.1.9 1.1.1.1 1.1.1.9 1.1.1.66 1.2.1.1 1.11.1.1

Replies are listed 'Best First'.
Re^2: How to sort IP addresses
by rinceWind (Monsignor) on Jan 30, 2007 at 10:36 UTC

    That's cool! Admittedly, you might be restricted as to perl versions, since it relies on vstring functionality. It occurred to me that there might be a golf like solution involving some kind of GRT. It's probably quite fast as well.

    I'll even name it for you: the Gutta Percha Transform :).

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      Hm, golfing with GRT? No idea how, but anyway, here's a possible starting point:
      $ perl -le '$,=$/; print @ARGV[ map { unpack N, substr $_, -4 } \ sort map { eval("v$ARGV[$_]") . pack N, $_ } 0 .. $#ARGV ]' \ 1.2.1.1 1.1.1.1 1.11.1.1 1.1.1.66 1.1.1.9 1.1.1.1 1.1.1.9 1.1.1.66 1.2.1.1 1.11.1.1
      OTOH, that previous version could easily be golfed (ok, just a little) by dropping the initial map (which is useful only when using this snippet "for real" - you know, using sprintf and returning a list):
      $ perl -e 'printf "%vd\n", $_ for sort map { eval "v$_" } @ARGV' \ 1.2.1.1 1.1.1.1 1.11.1.1 1.1.1.66 1.1.1.9 1.1.1.1 1.1.1.9 1.1.1.66 1.2.1.1 1.11.1.1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://597216]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2025-06-24 13:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.