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

Help!Ok I have an array full of IP address octets that need to be sorted efficiently without a foreach. THe array looks something like this
208.100.105
208.001.005
215.116.004
208.244.125
etc. (yes zero pads are intentional)
I want to use:
@sorted_array = sort efficiently @ip_addresses; sub efficiently { $a <=> $b; }
But its not working well... Problem I think is that the array is neither truely numerical because of the periods, nor a string... How to do this perl monks?! Thanks

Replies are listed 'Best First'.
Re: sort @array help
by davorg (Chancellor) on Dec 16, 2000 at 00:51 UTC
Re: sort @array help
by arturo (Vicar) on Dec 16, 2000 at 00:47 UTC
Re: sort @array help
by Adam (Vicar) on Dec 16, 2000 at 00:51 UTC
    in addition to arturo's comment, I would refrain from excessive use of <B>, it tends to annoy people.

    Oh, and also try reading things in the library, like sort.

    (Yes, that was an RTFM response, and yes, you asked for it.)

Re: sort @array help
by Kanji (Parson) on Dec 16, 2000 at 01:05 UTC

    If the octets in your array of IPs are all zero padded, is there really any need to bring a sorting block or sub into the picture?

    @sorted_array = sort @ip_addresses; should work just fine.

        --k.

Re: sort @array help
by I0 (Priest) on Dec 16, 2000 at 01:05 UTC
    Given your 0 padding,just:  @sorted_array = sort @ip_addresses; seems to do what you want.
Re: sort @array help
by mrmick (Curate) on Dec 16, 2000 at 00:52 UTC
Re: sort @array help
by Anonymous Monk on Dec 16, 2000 at 03:37 UTC
    Convert the IPS to integers (a single 32bit iteger, rather than a series of 4 8bit integers), sort them and then convert them back to dotted quad.
Re: sort @array help
by djw (Vicar) on Dec 16, 2000 at 01:04 UTC
    Sounds a lot like homework to me. "I need foo but I can't use a foreach". =)

    djw
Re: sort @array help
by turnstep (Parson) on Dec 16, 2000 at 03:27 UTC
    Problem I think is that the array is neither truely numerical because of the periods, nor a string.

    The "array" is nether a string nor numerical, it is simply an array. The elements within it, however, are all "strings" in this case.

    Update: Earlier misinformation removed. I blame it on the sugar. An honest thank you to whomever votes it down - I may not have noticed otherwise :)
Re: sort @array help
by symŽ (Acolyte) on Dec 16, 2000 at 01:10 UTC
    Hey, lay off you's guys. I am just a lowly programmer trying to make in this.....crazy world.... sniff.. :( lol thanks y'all
      Sorry, we never treat people badly around here, and it just happens that today everyone's feeling a little frisky.

      Seriously, though, I don't think that anybody feels like they should lay off, and I think you're going about it the wrong way. You asked for help on a question that was easily found in the search with many results. You got appropriate answers. Nuff said.

      Plus, nobody in their right mind would avoid foreach if it made sense to use it, which in this case, you can get away with sort @ip_addresses so you don't have to use it. You just happened to commit two personal sins of the monastery. Asking a question that has been answered to death, and phrasing it as if it was homework. (no judgement on the truth, but initially, I read it and though homework, esp. w/ the explicit use of ugly HTML tags in your post)

      Now, you've been flogged, and next time you post, you won't make the same mistake. We've all done it, and we all got over it.

      ALL HAIL BRAK!!!