[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Sort quickie




On Jul 7, 2004, at 3:07 PM, Miller, Scott L (Omaha Networks) wrote:
Another interesting possibility, expanding on what Jay started;
it might be possible to use Jay's technique to sort IP addresses
without first converting the addresses to their "long int" form...

cmp was driving me crazy so I jumped on IRC (irc.freenode.net #perl)...

The 2nd code block below is an IPv4 IP sorter for you... The 1st code block is a faulty one. -grin-

j


--------------------------------

This content is stored as  http://sial.org/pbot/3335.

From: "Omaha" at 68.13.20.113
Summary: Confused by cmp

I was writing a quick demo of how to sort IPv4 addresses. The problem is that the by_ip sort below should NOT work, as I undestand cmp, yet somehow it already does...

Shouldn't "sort @x" and "sort by_ip @x" as written below both return the same series? I thought '$a cmp $b' was the default behavior of sort?

Confused...

my @ips = qw(
   20.0.50.0
   20.0.100.0
   77.0.0.0
   100.0.0.0
);

print join ", ", sort @ips;
print "\n";
print join ", ", sort by_ip @ips;
print "\n";

sub by_ip {
   my ($a, $b) = @_;
   $a cmp $b;
}

----------

<pasteling> "Omaha" at 68.13.20.113 pasted "Confused by cmp" (23 lines, 554B) at http://sial.org/pbot/3335 <broquaint> Omaha: that's because you're comparing two undef vars i.e $a & $b aren't put in @_, they're magical package level vars
<broquaint> drop my($a,$b) = @_ and it works as expected
<Omaha> ... ahhh... ok. So my cmp always returned 0, leaving the array in original order, which just happened to be sorted by IP already. Got it... <Omaha> Any way to local($a, $b) so I can manipulate them? -ponder- Just trying to save a couple lines of code I guess...
<apeiron> Maybe more descriptive variable names would be a prudent idea.
* Omaha grins
<broquaint> avoid $a & $b outside of sort { ... }, it saves all sorts of headaches

-----------

This content is stored as  http://sial.org/pbot/3336.

From: "Omaha" at 68.13.20.113
Summary: sort by_ip -- feedback anyone?

Quick and dirty IPv4 sorter?

my @ips = qw( 20.0.100.0 20.0.50.0 100.0.0.0 77.0.0.0 );

print join ", ", sort @ips;
print "\n";
print join ", ", sort by_ip @ips;
print "\n";

sub by_ip {
   my ($j, $k) = ($a, $b);
   for ($j, $k) {
      $_ = sprintf("%03d.%03d.%03d.%03d", split /\./);
   }
   $j cmp $k;
}

------------------------

<pasteling> "Omaha" at 68.13.20.113 pasted "sort by_ip -- feedback anyone?" (17 lines, 327B) at http://sial.org/pbot/3336
<cfedde> Omaha: I'd use Sockets, inet_aton and {$a cmp $b}
<cfedde> Omaha: but yours works too

---------------

<apeiron> Beware False Hubris (inventing your own wheel), False Impatience (thinking you can make one more quickly than it'd take to learn an existing one), and False Laziness (thinking that making your own is less effort).

--------------

<nictuku> is this fine? if (!$lasttimestamp) { $lasttimestamp = 0; }
<Omaha> $lasttimestamp ||= 0;
<nictuku> Omaha, cool.
<Omaha> ya. :)