Sorting IP's 
Author Message
 Sorting IP's

I've got a quick perl question... Anyone here know of a way to sort IP's?
The only thing I can think of is to grep the IP's from my database... It's
a database of all our servers, and when I try to sort it by IP, it is
slow, because it has to grep such a long range of numbers...  If anyone
has any suggestions, please let me know...

dennis



Fri, 14 Jan 2000 03:00:00 GMT  
 Sorting IP's


Quote:

>I've got a quick perl question... Anyone here know of a way to sort IP's?
>The only thing I can think of is to grep the IP's from my database... It's
>a database of all our servers, and when I try to sort it by IP, it is
>slow, because it has to grep such a long range of numbers...  If anyone
>has any suggestions, please let me know...

IPv4 can have its addresses packed into 4 bytes, so you can do an (ASCII -
watch your locale...) sort on a string made up of the packed bytes e.g.

#!/usr/local/bin/perl  -w

              '122.5.5.99', '122.200.9.1', '127.0.0.1', '8.124.37.96',
             );




__END__

might be a quick and crude wat of doing it, or if you want to do a
Schwartzian transform on the list without using any named temporary files
then something like:

          sort {$a->[1] cmp $b->[1]}
          map {[$_, pack 'C4', split /\./, $_]}

might work for you.  Explanations of similar techniques can be found in
at

http://www.perl.com/CPAN//doc/manual/html/pod/perlfaq4/How_do_I_sort_...

and

http://www.perl.com/perl/everything_to_know/sort.html

Hope this helps,

Mike
--

http://www.stok.co.uk/~mike/       |   PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.tiac.net/users/stok/    |                   65 F3 3F 1D 27 22 B7 41



Sat, 15 Jan 2000 03:00:00 GMT  
 Sorting IP's


Quote:
> Anyone here know of a way to sort IP's?


Of course, you may need something fancier if your IP addresses aren't
packed into four bytes. :-)  But maybe you can see how to convert what you
have to be able to use this technique. Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/



Sat, 15 Jan 2000 03:00:00 GMT  
 Sorting IP's

Quote:

> I've got a quick perl question... Anyone here know of a way to sort IP's?
> The only thing I can think of is to grep the IP's from my database... It's
> a database of all our servers, and when I try to sort it by IP, it is
> slow, because it has to grep such a long range of numbers...  If anyone
> has any suggestions, please let me know...

> dennis

i wrote a sub that perform that, i don't promise you it's the fastest
way to do it but it works.

i have a file containing my work-stations which is formatted like this
and separated by ';'

=("Name","Srv","Own","Type","IP","CPU","OS","HD","Ram","Screen","Comment.");

example:
Barney;Tech;Lebeau james;DEC;192.63.123.45;Alpha;OSF;2;128;17;Stock
...
...

  if ($FORM{'sort'} eq "IP") {&sort_parc("4");}
..
..

sub sort_parc {

   if ($champsrt == 4) {
      # sort by ip numbers or if no ip found by name





          ($atmpd[3] <=> $btmpd[3]) || ($atmpd[2] <=> $btmpd[2]) ||
          ($atmpd[1] <=> $btmpd[1]) || ($atmpd[0] <=> $btmpd[0]) ||
          ($atmp[0] cmp $btmp[0]);

   } elsif ($champsrt == 1) {

          # sort by Services, by owner, by type and by name


             ($atmp[$champsrt] cmp $btmp[$champsrt])
          || ($atmp[2] cmp $btmp[2])
          || ($atmp[3] cmp $btmp[3])
          || ($atmp[0] cmp $btmp[0]);

   } elsif ($champsrt == 3) {
      # sort by machine cpu type and by name



          ($atmp[$champsrt] cmp $btmp[$champsrt])
       || ( substr($atmp[5],0,2) cmp substr($btmp[5],0,2) )
       || ($atmp[0] cmp $btmp[0]);

   } else {
      # sort by field (Alpha-numerical) and by name



          ($atmp[$champsrt] cmp $btmp[$champsrt]) || ($atmp[0] cmp
$btmp[0]);

   }

Quote:
}

if it could help !
--
***************************** Bertrand Le Guen              
*  "If at first you don't   * CAD/CAM & SGI/Unix Admin
*   succeed, destroy all    * Web Master (Intranet)

*****************************


Sat, 15 Jan 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Sorting IP's

2. Sorting IP's - Help!

3. ping all ip's fron one file without ip's from private sector

4. Sorting ip addrs (anyone) ?

5. sorting IP addresses?

6. Sorting an array of IP addresses

7. IP address incorrect by $ENV{'REMOTE_ADDR'}

8. IP's to DN's

9. Sorting IP addresses

10. IP address incorrect by $ENV{'REMOTE_ADDR'}

11. sorting IP addresses

12. Sorting IP numbers

 

 
Powered by phpBB® Forum Software