in reply to Re: Parsing of the web log file, access_log
in thread Parsing of the web log file, access_log


I have no problem subtracting dates as I use "localtime" and timelocal" routines provided by the standard Perl Module, "Time::Local".

Yes, I am having problem with how to output the records within an interval.

Infact, I wrote a subroutine, which I didn't put in the code that I posted. I know that it's not complete too.This is where I need help!

Here it is:
sub calculate_time { ($begin_Day,$begin_Month,$begin_Year,$begin_Hour,$begin_Minute,$beg +in_Second)= $begin_time =~m#^(\d\d)/(\w\w\w)/(\d\d\d\d):(\d\d):(\d\d) +:(\d\d)#; ($end_Day,$end_Month,$end_Year,$end_Hour,$end_Minute,$end_Second)= $da +teproc =~m#^(\d\d)/(\w\w\w)/(\d\d\d\d):(\d\d):(\d\d):(\d\d)#; &Initialize; my $begin_seconds = timelocal($begin_Second, $begin_Minute, $begin_ +Hour, $begin_Day, $MonthToNumber{$begin_Month}, $begin_Year-1900); my $end_seconds = timelocal($end_Second, $end_Minute, $end_Hour, $e +nd_Day, $MonthToNumber{$end_Month}, $end_Year-1900); my $elapsed = $end_seconds - $begin_seconds; if ( $elapsed < $interval ){ push (my @visual_page_values, {$processed_visual_pages{$dateproc +}}); print "The End seconds are: $dateproc @{$processed_visual_pages{$d +ateproc}}\n"; }else { $begin_time = $dateproc; push (@final_visual_pages, $dateproc); print " Final Visual pages are: @final_visual_pages\n"; } } sub Initialize { my %MonthToNumber=( 'Jan', '01', 'Feb', '02', 'Mar', '03', 'Apr', '04', 'May', '05', 'Jun', '06', 'Jul', '07', 'Aug', '08', 'Sep', '09', 'Oct', '10', 'Nov', '11', 'Dec', '12', ); my %NumberToMonth=( '01', 'Jan', '02', 'Feb', '03', 'Mar', '04', 'Apr', '05', 'May', '06', 'Jun', '07', 'Jul', '08', 'Aug', '09', 'Sep', '10', 'Oct', '11', 'Nov', '12', 'Dec', ); }

Replies are listed 'Best First'.
Re: Re: Re: Parsing of the web log file, access_log
by parv (Parson) on Jun 20, 2003 at 03:44 UTC

    I couldn't take the sub calculate_time as written. Below is a version w/ duplications reduced, does not solve the actual problem of printing data in the intervals (see OP).

    # Jun 20 2003 - create hash w/ "map" instead of explicit creation my %MonthToNumber; @MonthToNumber{qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)} = map { sprintf "%02d" , $_; } (1..12); my %NumberToMonth = map { $MonthToNumber{$_} => $_ } keys %MonthToNumber; sub calculate_time { my $get_sec = sub { my @time = reverse @{ parse_date($_[0]) }; return # second, minute, hour, day month year timelocal(@time[0..(scalar @time -3)] , $time[-2] -1 , $time[- +1]); }; my ($begin_sec , $end_sec) = ( $get_sec->($begin_time) , $get_sec->($dateproc) ); my $elapsed = $end_sec - $begin_sec; #printf "BEGIN: %s(%s) END: %s(%s)\nELAPSED: %s\n" # , $begin_sec , $begin_time # , $end_sec , $dateproc # , $elapsed; if ( $elapsed < $interval ) { push (my @visual_page_values, {$processed_visual_pages{$dateproc}} +); print "The End seconds are: $dateproc @{$processed_visual_pages{$ +dateproc}}\n"; } else { $begin_time = $dateproc; push (@final_visual_pages, $dateproc); print " Final Visual pages are: @final_visual_pages\n"; } } sub parse_date { my $date = shift; return [ ] unless defined $date; my ($day, $month, $year, $hour, $minute, $second) = split '[/:]' , $date; return [ $year , $MonthToNumber{$month} , $day , $hour , $minute , $second ]; }

    Other Notes (Jun 20 2003):

    • If parse_date() is not going to be used elsewhere, contents of the returned array reference should be reversed (to avoid reverse()-ing later for timelocal()).
    • Similar like above, if %MonthToNumber is used for the sole purpose to convert a month name to number for timelocal(), one could just use the hash values 0-11 instead of 1-12. In which case there also would be no need to use sprintf. More importantly, @time can be passed as it is, w/o the need of adjustment to any individual value.
    • %NumberToMonth seems unnecessary if/when it is employed few times, for some definitions of few.


      Thanks for that input and the code. I am actually looking at solving the interval problem. As I mentioned, I am able to get the timestamps with their no. of occurrences

      I shall appreciate any help in solving the interval problem.

      Per the suggestion of other posts, I tried to download Date::Calc, but not able to successfully do a "make". Pl. see my post in the Notes section.
Re^3: Parsing of the web log file, access_log
by tall_man (Parson) on Jun 20, 2003 at 00:27 UTC
    Whew! It's very hard to get details from you about what you are doing. I'm still not sure if you've shown me the part that you're having trouble with, because I don't see any code for finding the averages.

    However, I noticed a strange line here:

    push (my @visual_page_values, {$processed_visual_pages{$dateproc}});
    That "my" is scoped inside an if block and it won't be visible elsewhere. Also, for some reason you're creating a hash reference that has only one element, not a key/value pair.

    I notice at the start of your program that you commented out "use strict;" That's a very bad idea. I doubt you will be able to untangle the uses of "my" and global variables until you turn strict back on.