in reply to Problem with while loop
open (FILE, "$file") || die "Can't open $file"; $tmp=<FILE>; foreach(split(/\t/,$tmp)){ push(@hashkeys, $_); } while (<FILE>) { $tmp=$_;
The problem here is that you've slurpped the file into $tmp. So it can't read it again in your while loop unless you add the like:
seek(FILE,0,0);
That will start reading again at the beginning of FILE. That should allow you to loop through the contents of the file using while.
That said I think you are making this problem much harder than it should be. I do have a couple of questions though: You said that you were trying to change the colors of the page based on the amount of time that had passed. Time since what? Since the page was last accessed? Since it was updated?
Either way it sounds like you could benefit from HTML::Template (availiable from CPAN). Here's a short example that would change the background color based on the time of day:
#!/usr/bin/perl use strict; # Should always use this use HTML::Template; # Loads the HTML::Template module my $template = HTML::Template->new(filename=>"htmlbackground.tmpl"); my $hour = (localtime)[3]; my $bgcolor; if($hour < 12) { $bgcolor = "#00ff00"; # Green for the morning } elsif($hour == 12) { $bgcolor = "#0000ff"; # blue for noon } else { $bgcolor = "#ff0000"; # red for afternoon } # Set the template's variables to the varibles in the script $template->param(bgcolor=>$bgcolor); #Print the template out print "Content-type:text/html\n\n"; print $template->output(); exit;
Then in a separate HTML file called htmlbackground.tmpl:
<html><body bgcolor="<tmpl_var name=bgcolor>"> Hey I can change the background!</body></html>
When you call the script it substitutes <tmpl_var name=bgcolor> for the background color variable based on the time of day. If you would prefer to save it as a static file simply open a new file and print the template's contents to it rather than to STDOUT (which in this case is the webserver).
Hope that helps
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Oh my, code overload
by chromatic (Archbishop) on Oct 26, 2001 at 22:17 UTC | |
by Hopeless (Initiate) on Oct 26, 2001 at 22:47 UTC | |
Re: Oh my, code overload
by Anonymous Monk on Oct 26, 2001 at 22:09 UTC |