Forum Moderators: coopster & phranque

Message Too Old, No Replies

(perl) file as 15 lines or more ...

(perl) file as 15 lines or more ...

         

StopSpam

11:08 pm on Jul 7, 2003 (gmt 0)

10+ Year Member



I just buy the o'reilly perl cook book this day and it already pay it self back as i wrote this cool script using the book

this script opens a data file counts the amount of lines if data file reach 15 lines then the script deletes all lines.

i am not sure if it useful for anyone but it is to me ...

NOW i have one question and i cant find the answer so i hope some one can help me out ..

My question; the scripts deletes all lines if reach 15 lines... 15 = $cleanlist

but if data file contains 16 lines script do nothing
how can i say run &clean if is 15 or more ... not just 15 15 and plus or if $cleanlist or more ..

how do you write that?
i cant find it as i dont know for what to search in book or www

ps: if you run clean.cgi then it prints amount of lines
if you run clean.cgi?clean then it will remove all lines.
if you run clean.cgi and data file contains 15 lines then script auto delete all lines.
* What i want that it auto deletes lines if reach 15 or more ... but more aint working yet ;-(

############

#!/usr/local/bin/perl

$cleanlist = "15";

if ($ENV{'QUERY_STRING'} eq "clean"){ &clean}

open ( FH, "data.txt" ) ¦¦ die "Couldn't open that file\n";

while ( <FH> ) {
$count++;

}

print "Content-type: text/html\n\n";
print " file contains $count lines\n";

if ($count eq "$cleanlist"){ &clean}

sub clean {

open(FILE, "> data.txt") or die " Cant open data.txt: $!";
print FILE "";
close(FILE);

}

#################
God i love perl ...

this script is only 15 lines and it has to me 3 powerfull functions. count lines, clean lines and auto clean lines .. ;-) The world is probally spinning around powered by a perl script as well ehehehe .. with a cron job for day a nd night ...

sugarkane

3:31 pm on Jul 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'd need to use the >= operator (greater than or equal) in your 'if' statement, ie:

if ($count >= $cleanlist){ &clean}

instead of

if ($count eq "$cleanlist"){ &clean}

And also, when you're defining $cleanlist, it's better to do it without quotes as it's a numeric datatype you're defining (although perl is pretty forgiving about datatypes, it's better to be safe).

drbrain

4:47 pm on Jul 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'd better also check $ENV{'QUERY_STRING'} or somebody may pass something like eval("`rm -rf /`") (sorry if that's not correct, I don't know perl's syntax too well).