Greenguy's Board


Go Back   Greenguy's Board > Programming & Scripting
Register FAQ Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
Old 2005-08-18, 02:01 PM   #1
2ndChance
Aw, Dad, you've done a lot of great things, but you're a very old man, and old people are useless
 
2ndChance's Avatar
 
Join Date: Aug 2005
Posts: 20
Would any body know of a PERL equivalent to this PHP code?

Would any body know of a PERL equivalent to this php code that pulls a single line of data radomly from a txt file

Code:
<?
    $delim = "\n";
	$quotefile = "quotes.txt";
    // okay, let's get going. 
    $fp = fopen($quotefile, "r");
    $contents = fread($fp, filesize($quotefile)); 
    $quote_arr = explode($delim,$contents); 
    fclose($fp); 
    // initiate randomness
    srand((double)microtime()*1000000); 
    // generate random quote index 
    $quote_index = (rand(1, sizeof($quote_arr)) - 1); 
    // get quote at $quote_index and return it 
    $herequote = $quote_arr[$quote_index]; 
    echo $herequote; 
?>
Although this is a nice bit of randomness for php I need to impliment it in a perl script I have and well ... My brain isn't up to snuff to figure it out.

Thanks for at least taking the time to read this
2ndChance is offline   Reply With Quote
Old 2005-08-18, 02:05 PM   #2
2ndChance
Aw, Dad, you've done a lot of great things, but you're a very old man, and old people are useless
 
2ndChance's Avatar
 
Join Date: Aug 2005
Posts: 20
and no I didn't wirte the above code I got it out of another script "random quotes" Though I use it for random link creation with a great deal of success.
2ndChance is offline   Reply With Quote
Old 2005-08-18, 02:34 PM   #3
cd34
a.k.a. Sparky
 
cd34's Avatar
 
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
Code:
#!/usr/bin/perl

open WL,"wordlist";
chomp(my @words = <WL> );
close WL;

print $words[rand(@words)];
something like that.
__________________
SnapReplay.com a different way to share photos - iPhone & Android
cd34 is offline   Reply With Quote
Old 2005-08-18, 02:45 PM   #4
2ndChance
Aw, Dad, you've done a lot of great things, but you're a very old man, and old people are useless
 
2ndChance's Avatar
 
Join Date: Aug 2005
Posts: 20
lol Thanks
often the simple things confound me
I got it now thanks again
2ndChance is offline   Reply With Quote
Old 2005-08-19, 08:58 PM   #5
2ndChance
Aw, Dad, you've done a lot of great things, but you're a very old man, and old people are useless
 
2ndChance's Avatar
 
Join Date: Aug 2005
Posts: 20
SO I thought I would post what I did
Code:
open (RANDOMLINKS,"$data_server_path$randomlinks") || print "Could not open $data_server_path$randomlinks $! \n";
@FILE = <RANDOMLINKS>;
close(RANDOMLINKS);
$links = join('',@FILE);
@links = split(/\n/,$links);
srand(time ^ $$);
$link = rand(@links);
print "<BR>$links[$link]";
$link2 = rand(@links);
print "<BR>$links[$link2]";
$link3 = rand(@links);
print "<BR>$links[$link3]";
This will pull three random lines from a text file spit them out.
If anybody wants to use this as a randomizer by itself contact me and I will send it to you in a zip file configured so you can use it.

Last edited by 2ndChance; 2005-08-19 at 09:12 PM.. Reason: added details
2ndChance is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:17 PM.


Mark Read
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc