CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
rand

 



perlkid
stranger

Feb 16, 2000, 9:26 PM

Post #1 of 4 (655 views)
rand Can't Post

 
How can I get my search results to display randomly? I need to know how to print the matches that are stored in an array randomly.

Thanks,
perlkid


darian
Deleted

Feb 16, 2000, 9:46 PM

Post #2 of 4 (655 views)
Re: rand [In reply to] Can't Post

Try something like this. I used this in an old webring (actually named it WEBchain) script I wrote.

open(LINKS, "<$file") &#0124; &#0124; &Error("cant open $file");
@entries = <LINKS>;
close LINKS;
srand (time|$$);

$entry = rand(@entries);
@link = $entries[$entry];
foreach $line (@link) {
@fields = split(/\|/, $line);

print "Location: $fields[3]\n\n"; # print a random line
}

I split each line as there was more information than just the url that I wanted. You will probably have to play around with it a bit but it comes up with a random url each time.


perlkid
stranger

Feb 17, 2000, 2:23 AM

Post #3 of 4 (655 views)
Re: rand [In reply to] Can't Post

 
Sweet,

Thanks for the code. That will work just fine.

thanks,
perlkid


japhy
Enthusiast

Feb 17, 2000, 4:05 AM

Post #4 of 4 (655 views)
Re: rand [In reply to] Can't Post

To assign the elements in an array randomly, you can do:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


push @array, (splice @array, rand(@array-$_), 1) for 0 .. $#array;
</pre><HR></BLOCKQUOTE>

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives