Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Oh my, code overload

by cfreak (Chaplain)
on Oct 26, 2001 at 20:14 UTC ( #121658=note: print w/replies, xml ) Need Help??

in reply to Problem with while loop

I think I'd have to pour over this code awhile to figure out exactly what it is that's going wrong. I did notice one thing though.
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:


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
    No, that's wrong. "Slurp mode" is only in effect when $/ has been localized or otherwise set to an undefined value. A filehandle READLINE in scalar context only reads a single line. seek is unnecessary, and is, in fact, probably counterproductive.

    It looks to me like the OP has a tab-delimited flatfile. The first line probably lists the headers. The code grabs the field names from the first line with the first read to use as hash keys. Then it loops through the rest of the lines -- the actual data. It's not the most beautiful idiom, but it's valid code, if this is actually the poster's intent.

      Yeah, that sounds like what I am trying to do
Re: Oh my, code overload
by Anonymous Monk on Oct 26, 2001 at 22:09 UTC
    I am comparing sync times for files that are replicated in different locations, I compare the time of the last import/export to the system clock and based on the result the name of the replica returns Green < 3(hours), Yellow (3-7 hours) or Red(greater than 7 hours). It will do this if I take out the second while loop but then I can't separate the 2 different sites($ABC & $DEF) and print them in nice orderly columns that are pleasing to the eye. I hope that helps clear up some things

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://121658]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2022-11-30 19:38 GMT
Find Nodes?
    Voting Booth?