Jennifer Garner wrote:
hi,lists,

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;

                    # Accumulate the totals directly
                    if( $bit < 128 ){
                      $total{$net}{low} ++;
                    }else{
                      $total{$net}{high} ++;
                    }
                    $total{$net}{total} ++;

                }
        }
        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";
}


OK, we're going to use a technique called the Schwartzian transformation, named after our good friend Randal L. Schwartz, who invented it.

foreach (
  map { $_->[0] }
  sort { $b->[1] <=> $a->[1] }
  map { [ $_, $total{$_}{total} ] }
  keys %total
){
print RESULT "$_\t$total{$_}{low}\t$total{$_}{high}\t$total{$_}{total}\n";
}


--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
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