Gossamer Forum
Home : General : Perl Programming :

Random line

Quote Reply
Random line
I need I small peze of perlcode that take a file and then random take a line from it and put it in a variable.
Quote Reply
Re: Random line In reply to
How about:

srand;
open (FILE, "file.txt") or die $!;
rand($.) < 1 && ($line = $_) while <FILE>;
close FILE;

from the perl faq:

http://language.perl.com/...ect_a_random_line_fr

The random line will be in $line.

Cheers,

Alex
Quote Reply
Re: Random line In reply to
What about taking that file and pulling multiple numbers out of it at random?

When I try to do this, even using the whole sequence over again (printing it twice in the script so that it will open the file two different times and choose) it gives me the same number... repeated twice.

Ideas?
Raven
Quote Reply
Re: Random line In reply to
I've *kinda* gotten around that problem.

What I'm doing is making a random dice roller for a friend who roleplays.
The user's interface is an html page with a form where the user puts in their name (I do this in plans to make the script log entries so that someone else can check for cheating by looking at the log), then chooses the side of dice to roll and the number of dice to roll.

Right now it doesn't matter what you choose because I haven't gotten that far yet. Smile
But I'm progressing!

As it stands I have two text files with numbers 1-10 listed in them in a different order (I found that when I put them in the same order it picked the same number). The script uses the snippet of code Alex posted above... twice. Each snippet opens a separate file, chooses a number, and prints it.

Here's my prob... proper roleplay can have up to 20 dice being rolled at once.
That would mean 20 text files for each die they can select (4,6,8,10,12,20,30,100) and I could have 20 files being opened and closed at once!
There's gotta be a shorter way to do this, but if it works... 8)

Raven
Quote Reply
Re: Random line In reply to
Alex, how about this:

Code:

open (FILE, "file.txt");
@LINES=<FILE>;
close(FILE);
srand;
print "Content-type: text/html\n\n$LINES[int rand(@LINES)]\n";

works fine for me! Smile


Regards,

Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net
Quote Reply
Re: Random line In reply to
By the way, I can't think of a code, but I really need it. I got a database file with structure similar to 'links.db' file, what I need is the simple script, that could read all the fields, and write them to the HTML file:

Code:

open (DATABASE, "file.txt");
@database_in=<DATABASE>;
foreach $_ (@database_in)
{
($first,$last,$tel,$address,$info)=split(/|/);
{
$output=$output."<tr><td>$first</td><td>$last</td><td>$tel</td><td>$address</td><td >$info</td></tr>\n";
}
}
{
print "<table>";
print $output;
print "</table>";
}


There supposed to be an easier way to do this, plus I need users to read the HTML file without executing CGI, so it have to be printed to the HTML file:


Code:

print "Location: /04/index.htm\n\n";

Does any one have this or similar code?
Thank you in advance.


Pasha

------------------
webmaster@find.virtualave.net
http://find.virtualave.net