dru145 has asked for the wisdom of the Perl Monks concerning the following question:
192.168.17.10 was seen 148 times http was seen 148 times 192.168.12.187 was seen 148 times ftp was seen 148 times
#!/usr/bin/perl -w use strict; my $log = './logfile'; my (@data, @service, @dst, %count, %counts, %hash, %sizes, $dst, $serv +ice, $ip, $times); &check("192.168.47.3"); sub check { $ip = $_[0]; open (LOG, $log) or die "Can't open $log: $!"; while (<LOG>){ push (@data, $_) if $_ =~ /$ip/; foreach (@data){ ($dst, $service) = (split /;/)[11,12]; next if m/^\s*$/; #skip any empty lines push(@service, $service); push(@dst, $dst); } } @service = &duplicates(@service); @dst = &duplicates(@dst); foreach (@data){ foreach my $i (@dst){ $count{$i}++; } foreach my $j (@service){ $count{$j}++; } } } while (my ($key, $count) = each(%count)){ print "$key was seen $count times\n"; } sub duplicates { my @array = @_; my %saw; @array = grep(!$saw{$_}++, @array); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid) Re: Number of times each element in an array appears.
by Ovid (Cardinal) on Jan 01, 2002 at 01:19 UTC |