Gossamer Forum
Home : General : Perl Programming :

Is there a script for this?

Quote Reply
Is there a script for this?
I want to open up a text file, say "textfile1" with information on each line (say about 100 lines) and select about 10 lines at random and write (output) these 10 lines to another "pre-defined" text file let's say textfile2 in the same directory, completely replacing the previous 10 ie. before the script is run the first time "textfile2" will be blank. From thereon the previous 10 entries should be replace by new 10 entries, each time the script is run.

Anyone knows?
Thanks

PS. My perl knowledge is a big "0"
Quote Reply
Re: Is there a script for this? In reply to
No, there isn't a script for that exactly, but one could be created. I would start with one of the random text scripts that you will find at http://cgi.resourceindex.com/...ts/Perl/Random_Text/ , most likely Matt's one - http://www.worldwidemart.com/scripts/rand_text.shtml , for simpicity. Ok, it's displaying the text rather than writing it, but it wouldn't be too hard to change.

I don't have time to go into it now, but have a look at the script and I'll try and help out over the next couple days.

adam
Quote Reply
Re: Is there a script for this? In reply to
Thanks, but that does not help. I am aware of the random text which prints to the browser. What I am looking for is something that writes random lines back to another "file". Here is what I have.


#!/usr/bin/perl
$file = "/cgi-bin/data/input.txt";
$datafile = "/cgi-bin/data/output.txt";
use LWP::Simple;

open (DATA, ">$file") or &cgierr("Error: Unable to open $datafile. Reason: $!");

flock(DATA, $lock);
while(<DATA> ) {
@data =
THE CODE FOR RANDOM FILE SHOULD BE HERE

flock(DATA, $unlock);
close (DATA);
chmod (0666, "$datafile") or &cgierr("Error: Unable to chmod $datafile. Reason: $!");
exit;
}
sub cgierr {
print "Content-type: text/html\n\n";
print "$_[0]\n";
}