Some "simple" solutions for you.
Be aware that the Perl rand() function is not completely random - works ok for most situations but is not really "random" - there are biases in this (odd vs even), etc.
If you want to use rand() to plan Casino strategies, there are better number generators!
But this is one way to get the job done. I do recommend buying a copy of the Perl Cookbook.
#!/usr/bin/perl -w
use strict;
#pick a "random arg from a list"
my @names = qw(ny.txt ca.txt nj.txt pa.txt ma.txt wa.txt);
my $random_file = $names[rand(@names)]; #UPDATE: previous code had
#an "off-by-one" error, Oops!
print "$random_file\n";
Pick A Random Line from a file:
From:
Perl Cookbook
#Chapter 8.6. Picking a Random Line from a File
#Problem: You want to return a random line from a file.
#Use rand and $. (the current line number) to decide
#which line to print:
srand;
rand($.) < 1 && ($line = $_) while <>;
Exactly how this works is complicated.
The "Cookbook" is a great book and highly recommended by me.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|