Your problem is in this loop:

foreach (@data){ foreach my $i (@dst){ $count{$i}++; } foreach my $j (@service){ $count{$j}++; } }

Since you apparently have 148 lines of @data, you're simply counting that for each destination and service. Count them when they come in:

#!/usr/bin/perl -w use strict; my $log = './logfile'; my (@data, %count); my $count = &check("192.168.47.3"); sub check { my $ip = $_[0]; my %count; # open LOG, "< $log" or die "Can't open $log: $!"; while (<DATA>){ push (@data, $_) if $_ =~ /$ip/; } # close LOG; foreach (@data){ my ($dst, $service) = (split /;/)[11,12]; $count{ $service }++; $count{ $dst }++; } return \%count; } while (my ($key, $count) = each(%$count)){ print "$key was seen $count times\n"; } __DATA__ 0;1;2;3;4;5;6;7;8;9;10;onedest;http;192.168.47.3 0;1;2;3;4;5;6;7;8;9;10;twodest;http;192.168.47.3 0;1;2;3;4;5;6;7;8;9;10;onedest;http;192.168.47.3 0;1;2;3;4;5;6;7;8;9;10;onedest;http;192.168.47.3 0;1;2;3;4;5;6;7;8;9;10;onedest;ftp;192.168.47.3

Output:

twodest was seen 1 times http was seen 4 times ftp was seen 1 times onedest was seen 4 times

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Number of times each element in an array appears. by Ovid
in thread Number of times each element in an array appears. by dru145

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.