Chad Kellerman wrote:
> 
> Hello,

Hello,

>     I wrote a script that goes thru the a access logs of a web server
> and prints out the hour and number of hits/hour.  THe only problem is
> that if i use warnings I get a bunch of errors when I count the hits.
> 
> Use of uninitialized value in addition (+) at scripts/hits.pl line 14,
> <IN> line 20569.
> 
> #!/usr/bin/perl
> 
> use warnings;
> use strict;
> 
> #define variables
> my %hits;
> 
> open IN, '<', $ARGV[0] or die "cannot open log file: $!";
> 
> while (my $line = <IN>) {

Since you are reading from a file listed on the command line you could
use the <> file handle.

>      chomp $line;

Since you are extracting data from the middle of the line there is no
reason to remove newlines.

>      my (undef, $hour,undef) = split(/:/, $line);
>      $hits{$hour} = $hits{$hour}+1;
> }
> 
> foreach my $hour (sort keys %hits) {
>      print "$hour \+ -\+".$hits{$hour},"\n";
> }
> 
> The problem lies in the line that says:
> 
> $hits{$hour} = $hits{$hour}+1;
> 
> What it the best way to create a counter?
> 
>   I have seen $var++, it would not work here I had to use +1.


#!/usr/bin/perl
use warnings;
use strict;

#define variables
my %hits;

while ( <> ) {
    $hits{ (split /:/)[1] }++;
}

for my $hour ( sort keys %hits ) {
    print "$hour + -+$hits{$hour}\n";
}



John
-- 
use Perl;
program
fulfillment

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

Reply via email to