: The last_lowest is the last value written out to the file.
 : The tmp value is the current value plucked from the list.
 : My hope by going through all the values and picking every one that
 : is larger than the last smallest one, the next one will be grabbed.
 : 
 : By the way, I can't get Perl's sort function to do the right thing
 : exactly either.
 : 
 :   my @sorted_ips=sort {$a <=> $b} @ip_list;
 : 
 : This command sorts the first octet correctly, but not the second, 
 : third, or fourth octets.  The numbers are in binary format when 
 : sort is run on them.

You have said in several posts that you want to sort them in binary 
order.  I'm really not sure what that means, but I understand you to 
mean that you wish to use the integer representation for sorting 
your prefixes.

You could (as somebody had pointed out earlier) use the Net::IP 
module, which provides quite a few features.  If you want only 
sorted IPs, there are some venerable tools at your disposal.  I 
would suggest using the inet_aton call to turn the IP into a number.

  #! /usr/bin/perl
  #
  # -- sort IPs by integer representation
  
  use strict;
  use Socket;
  
  my @ips = readline(STDIN);  # -- slurp
  chomp(@ips);
  
  @ips =
    map { $_->[1] }                                 # -- get second list elem
    sort { $a->[0] <=> $b->[0] }                    # -- sort on first list elem
    map { [ unpack( "N", inet_aton( $_ ) ), $_ ] }  # -- make IP an integer
    @ips;
  
  print join("\n",@ips),"\n";
  
  # -- end of file

Above is a so-called Schwartzian transform on a small list of IPs 
(up to a few hundred thousand should be trivial.  I ran the above on 
a set of 15 million IPs I had sitting around which required about 
9GB of RAM to run.  The above snippet will not win any efficiency 
prizes, but it should accomplish what you want.

 : I don't undersand the special syntax on the sort command, but I 
 : suspect that I need something different there.

 : The beauty of the sort function, if it will work, is that I can 
 : replace an entire function that is fairly long in comparison.  
 : This is why I'm using perl after all, it is supposed to be 
 : designed to do this sort of thing.

The usage of $a and $b in the sort function isn't intuitive--you 
could say it's almost weird, but it is pretty powerful.  If you 
haven't already, try 'perldoc -f sort'.  There are quite a few 
examples.

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
_______________________________________________
PLUG mailing list
PLUG@lists.pdxlinux.org
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to