How to Hash in Perl -
i finding uniques url in log file along response stamp can available using $line[7]. using hash unique urls.
- how can count of unique url?
- how can average of response time along count of unique url?
with below code getting
url1 url2 url3 but want along average response time , count of each url
url av.rt count url1 10.5 125 url2 9.3 356 url3 7.8 98 code:
#!/usr/bin/perl open(in, "web1.txt") or die "can not open file"; # hash store final list of unique ips %uniqueurls = (); $z; # read log file line line while (<in>) { @line = split(" ",$_); $uniqueurls{$line[9]}=1; } # go through hash table , print keys # unique ips $url (keys %uniqueurls) { print $url . "\n"; }
store listref in hashing directory:
$uniqueurls{$line[9]} = [ <avg response time>, <count> ]; adjust elements accordingly, eg. count:
if (defined($uniqueurls{$line[9]})) { # url known, increment count, # update average response time data current log entry $uniqueurls{$line[9]}->[0] = (($uniqueurls{$line[9]}->[0] * $uniqueurls{$line[9]}->[1]) + ($line[7] + 0.0)) / ($uniqueurls{$line[9]}->[1] + 1) ; $uniqueurls{$line[9]}->[1] += 1; } else { # url not yet known, # init count 1 , average response time actual response time log entry $uniqueurls{$line[9]} = [ $line[7] + 0.0, 1 ]; } to print results:
# go through hash table , print keys # unique ips $url (keys %uniqueurls) { printf ( "%s %f %d\n", $url, $uniqueurls{$url}->[0], $uniqueurls{$url}->[1]); } adding 0.0 guarantee type coercion string float safeguard.
Comments
Post a Comment