Rmck wrote:
> 
> HI

Hello,

> I have a script that reads stdin from the output of another script
> and cleans it up and prints it. The script gets ip's.
> 
> I would like to sort it and and eliminate duplicates count the
> sorted/unique ips then print???

The best way to store unique values is to use a hash.

> I thought using the perl sort command would help but it did not...
> I could pipe the output to /bin/sort -u, but I wanted to do everthing
> in perl.
> 
> 
> #!/usr/bin/perl
> use strict;
> my $count = 0;
> 
>    while( <> ) {                       #read from stdin one line or record at a time
> 
> s/ipadd://;                              #if line has ipadd: remove it
> s/^ |\t//;                                #if any whitespace at the beginning of 
> string rm
> next if ($_=~/^\s*(\*|$)/);      #if line begins with a *
> 
> print sort $_;

You are sorting a single value, not a list of values.

> $count ++;                             # Count
> 
>   }
> print "\n";
> print "Total IP'S = $count\n";
> 
> Current out:
> 111.222.81.97
> 111.222.81.97
> 111.111.135.11
> 
> Total IP'S = 3
> 
> Goal Out:
> 111.111.135.11
> 111.222.81.97
> 
> Total IP'S = 2


I would do it something like this:

#!/usr/bin/perl
use warnings;
use strict;
use Socket;

my %IPs;
while ( <> ) {
    $IPs{ inet_aton( $1 ) }++ if /\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/;
    }

for ( sort keys %IPs ) {
    print inet_ntoa( $_ ), "\n";
    }

print "\nTotal IP'S = ", scalar keys %IPs, "\n";

__END__



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