On May 23, A. Rivera said:

>On May 23, Craig Hammer said:
>> I am working on a script to read in a firewall logfile, pull out the IP
>> addresses of denied packets, then give me a count per IP address, and
>> perform a whois on each address.
>>
>> This previously ran as a VERY SLOW shell script.  In bourne, I used sort
>> andthen uniq to get a count per IP address.  Is there something similar
>> to uniq within perl?  (I already have it sorting correctly)
>
>sub uniq {
>  my @in=@_;
>  my (%saw,@out);
>  undef %saw;
>  @out = grep(!$saw{$_}++, @in);
>  return @out;
>}
>
>Unfortunately, I have no idea how it works.

It uses post-increment and the fact that hash keys are unique to do its
work:

  for (@in) {
    if (! $saw{$_}++ ) { push @out, $_ }
 }

Post-increment returns the value first, and THEN increments it.

  $x = 3;
  print $x++;  # 3
  print $x;    # 4

Thus, $saw{$_}++ will return 0 the first time $_ is searched for in the
hash.  !0 is 1 (true), so the first time $_ is searched for, it is pushed
to the @out array AND it gets a value in the hash of 1.  The next time
it's found $saw{$_}++ returns 1 (and sets $saw{$_} to 2), and !1 is 0
(false) so $_ is not pushed a second time.

Craig -- which makes more sense:

  a) sort a 1000 records, then remove duplicates; or
  b) remove the duplicates, then sort the remaining records

The answer is, you can't be sure.  It probably depends GREATLY on your
data.  Here are solutions for both a) and b).

  # a -- from perlfaq4 (perldoc -q uniq)
  my $prev = "NO_SUCH_VALUE";
  my @sorted = grep { $_ ne $prev and $prev = $_ } sort @records;

and

  # b -- also from perlfaq4
  my %seen;
  my @sorted = sort grep !$seen{$_}++, @records;

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to