Jennifer Garner wrote:
> hi,lists,

Hello,

> I have a file which is so large,which looking as:
> 
> 61.156.49.18:28360
> 61.183.148.130:27433
> 222.90.207.251:25700
> 202.117.64.161:25054
> 218.58.59.73:24866
> 221.233.24.9:22507
> 222.187.124.4:21016
> ...
> 
> and more than 45000000 lines.
> 
> the part after ":" is no use for me,I only need the IP.
> 
> for each IP,such as '218.58.59.73', I want to get this result:
> 
> 218.58.59.              xxx             yyy             xxx+yyy
> 
> I want to know how many IP are in the range of '218.58.59.1' to '
> 218.58.59.127',this is 'xxx';
> and how many IP are in the range of '218.58.59.128' to '218.58.59.254',this
> is 'yyy'.
> 
> I write this code:
>     open (FILE,$file) or die "$!";
>         while(<FILE>)
>         {
>                 next if /unknown/o;
>                 next if /^192\.168\./o;
>                 chomp;
>                 my ($ip,$num) = split/:/,$_;
>                 if ($ip = ~  /^(\d+\.\d+\.\d+\.)(\d+)/o){
>                     my ($net,$bit) = ($1,$2);
>                     $total{$net}{low}{$bit} = 1 if $bit < 128;
>                     $total{$net}{high}{$bit} = 1 if $bit >=128 and $bit <
> 255;
>                     $total{$net}{total}{$bit} = 1;
>                 }
>         }
>         close FILE;
> 
> foreach (sort { scalar keys %{$total{$b}{total}} <=> scalar keys
> %{$total{$a}{total}} } keys %total)
> {
>     print RESULT "$_","\t",scalar keys %{$total{$_}{low}},"\t",
>               scalar keys %{$total{$_}{high}},"\t",scalar keys
> %{$total{$_}{total}},"\n";
> }
> 
> 
> but it's too slow for me to wait the result.How can I get it more effective
> and run less time?thanks.

This is quite a bit faster then your version:

open FILE, '<', $file or die "Cannot open '$file' $!";

my ( %low, %high, %total );
while ( <FILE> ) {
    next if /unknown/;
    next if /^192\.168\./;

    next unless /^(\d+\.\d+\.\d+\.)(\d+)/;

    if ( $2 < 128 ) {
        $low{ $1 }++;
        }
    else {
        $high{ $1 }++;
        }

    $total{ $1 }++;
    }

close FILE;


for ( sort { $total{ $b } <=> $total{ $a } } keys %total ) {
    print RESULT "$_\t", $low{ $_ } || 0, "\t", $high{ $_ } || 0, "\t",
$total{ $_ }, "\n";
    }


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to