Hi I'm trying to print to an HTML file but I'm having some issues calling a variable because it doesn't get defined until later in the script. I have a log file and I need to calculate the number of unique visitors as well as its start date (the earliest date) and then print that information at the top of the HTML file. My problem is those variables aren't defined yet! How can I make this work?

#! /usr/bin/perl # CSC 310 Project # 4 use 5.010; use Time::Local; #opening log and html file open (LOG, '<', 'IN-access.log'); open (HTML, '>', 'OUT-access.html'); #printing header/title/format of html print HTML "<HTML><TITLE>Visitors Log</TITLE><BODY>\n"; print HTML "The log file start date is: $startDate <BR>\n"; print HTML "There were $IPcount unique visitors in the logfile.<BR>\n" +; print HTML "There were visits yesterday<BR>\n"; print HTML "<TABLE border=1><TR><TD>IP</TD><TD>LOGFILE</TD></TR>\n"; ############## #replaces the month with its corresponding number sub convert{ ($day,$month,$year) = split '/', $formattedDate; #print "$day $month $year\n"; %dates = ( 'Jan' => '00','Feb' => '01','Mar' => '02', 'Apr' => '03','May' => '04','Jun'=> '05', 'Jul' => '06','Aug' => '07','Sep' => '08', 'Oct' => '09','Nov' => '10','Dec' => '11', ); foreach $char ($month){ $char =~ s/.../$dates{$month}/; } $modified = "$day/$month/$year"; $modified; } ############### #reading log file while ($lines = <LOG>){ #assigning values ($remoteIP,$rfc,$userID,$dateTime,$timeZone,$requestType,$fileRequ +ested,$requestProtocol,$statusCode,$sizeOfFile) = split ' ', $lines; ####### converting month to a number, then the date to epoch time $formattedDate = substr($dateTime, 1, 11); #gets hrs,mins,secs from $dateTime ($hr,$min,$sec) = split ':', substr($dateTime, 13,8); #converts month to corresponding number $modifiedDate = &convert($formattedDate); #open (HEAD, '>>', 'ipDate.txt'); #print HEAD "$remoteIP,$modifiedDate\n"; ($DAY,$MONTH,$YEAR) = split '/', $modifiedDate; $logDate = timelocal($sec,$min,$hr,$DAY,$MONTH,$YEAR); push(@listOfDates, $logDate); @sortedDates = sort {$a <=> $b} @listOfDates; #start date of log file my $startDate = $sortedDates[0]; print scalar(localtime($startDate))."\n"; #keeps track of unique visitors #checks if remoteIP is already in the array. If not, counter increa +ses #and remoteIP gets added to array if ($remoteIP ~~ @listOfIPs){ $IPcount = $IPcount; }else{ $IPcount += 1; push(@listOfIPs, $remoteIP); } #verifies values were assigned properly print "\nUnique Visitors: $IPcount\n"; print "$remoteIP\n"."$rfc\n"."$userID\n"."$dateTime "."$timeZone\n +"."$requestType $fileRequested $requestProtocol\n"."$statusCode\n"."$ +sizeOfFile\n\n"; #printing data to HTML file print HTML "<TR><TD>$remoteIP</TD><TD>$remoteIP $rfc $userID $date +Time $timeZone $requestType $fileRequested $requestProtocol $statusCo +de $sizeOfFile</TD></TR>\n"; print "\n\nNEXT\n\n"; }

In reply to Possible to call a variable before its defined in while loop? by jaffinito43

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.