Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Environment Variables

by n4mation (Acolyte)
on Jan 03, 2002 at 09:38 UTC ( [id://135922]=perlquestion: print w/replies, xml ) Need Help??

n4mation has asked for the wisdom of the Perl Monks concerning the following question:

I run a very simply perl stats script on different servers, that stores environment variables in a text file. Works fine on unix server, but when I run it on the windows server it stores 2 lines in the file for one visitor. Example: The referer variable gives the url from the external site, and it gives my url as the referer to the cgi script. This is really no prob until I display the file in a html table. Could someone tell me how to eliminate that extra line, or how to display every other line in the html table using the following code, or facsimile?
open (DB, "<$data_file"); while (<DB>) { $row = $_; chop $row; @fields = split (/\|/, $row); print "$fields[0]"; } close (DB);
I just use a simple SSI to call the scripts. Thanks in advance.

Replies are listed 'Best First'.
Re: Environment Variables
by dmmiller2k (Chaplain) on Jan 03, 2002 at 09:55 UTC

    First, you may want to switch from calling chop() to chomp() instead (the latter will only strip off an end-of-line character, as defined by the variable $/).

    Next you probably don't need the unnecessary variable $row, since both chomp() and split() operate on $_ implicitly.

    Now, to answer your question, you can skip every other line by testing $. for odd/even-ness (use experimentation to decide which one -- odd or even -- you need to skip). For example, if the first line and every *other* line thereafter should be skipped:

    open (DB, "<$data_file") or die "$data_file: $!"; while (<DB>) { next if $. % 2; # change 'if' to 'unless' to skip even lines chomp; my @fields = split /\|/; print "$fields[0]"; } close DB;

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime
Re: Environment Variables
by mrbbking (Hermit) on Jan 03, 2002 at 18:25 UTC
    Is the format of the file 100% reliable? That is, if skipping even-numbered lines works today (or one *one* server), will it necessarily work tomorrow (or on *another* server)?

    If so, then I'll second dmmiller2k's answer.

    If not, then you might want to consider using a regex to match the lines you want to omit. It will probably take more time than testing $., so you might want to use Benchmark, if that's a concern for your application. Assuming you want to omit the referer line that lists your own URL, you could do something like this:

    open (DB, "<$data_file"); while (<DB>) { next if m!www.yourserver.com/your/referer/here!; $row = $_; chop $row; @fields = split (/\|/, $row); print "$fields[0]"; } close (DB);
Re: Environment Variables
by archen (Pilgrim) on Jan 03, 2002 at 19:19 UTC
    This really doesn't have much to do with your question, but is more a style thing... but anyway, why do you chop $row when you're just using the first item returned by split? As far as I can tell this doesn't really help... Anyway, it might be something to think about.
    while(<DB>) { print ( split(/\|/) )[0]; }
    Update: Oh yeah, this code just does the same thing as yours (so nothing new here). See posts above and below for actual insightful information :p

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-24 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found