Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Possible to call a variable before its defined in while loop?

by jaffinito43 (Initiate)
on Nov 13, 2012 at 01:07 UTC ( #1003532=perlquestion: print w/ replies, xml ) Need Help??
jaffinito43 has asked for the wisdom of the Perl Monks concerning the following question:

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"; }

Comment on Possible to call a variable before its defined in while loop?
Download Code
Re: Possible to call a variable before its defined in while loop?
by davido (Archbishop) on Nov 13, 2012 at 01:28 UTC

    Is this a joke, or did you forget that you already asked the question here: Possible to refer to variable defined later in script??

    My suggestion is that you follow up in that thread, clarifying what it is that you are still confused about after you've taken the time to read and study the responses you already have there.


    Dave

      ... you already asked the question ...

      Clearly, this is another monk, jaffinito43 versus jaffinito34, although, to be sure, they're both working (together?) on CSC 310 Project #4!
      (Update: And they seem to have been joined by jrp370.)

Re: Possible to call a variable before its defined in while loop?
by roboticus (Canon) on Nov 13, 2012 at 11:49 UTC

    jaffinito43:

    One morning, I turned on the light switch, and the light bulb burned out. Since I didn't have any light bulbs, I needed to go to the store and buy some. I couldn't find my keys in the dark, though.

    How did I solve this dilemma? Simple! I put a new bulb in the lamp, turned it on, then drove to the hardware store to buy the bulb that I just put in the lamp.

    Recently, I've been solving all my programming problems like that--When I get a new assignment, I print out the program so I can see what's in it, and then just type it in!

    </snark>

    ...roboticus

    Causality schmauzality... Meh.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003532]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-07-10 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls