I think I'm close at finding how many times an element appears in the log file from each array, but I am getting the same results for each element. Here is my output when the script is ran:
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
What am I doing wrong? Here is the code:
#!/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);
}
Thanks,
Dru
Another satisfied monk.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.