in reply to Opening random files then reading random lines from file.
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.
Pick A Random Line from a file:#!/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";
Exactly how this works is complicated.#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 <>;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Opening random files then reading random lines from file.
by jwkrahn (Abbot) on Apr 27, 2012 at 06:24 UTC | |
by Marshall (Canon) on Apr 27, 2012 at 06:42 UTC | |
|
Re^2: Opening random files then reading random lines from file.
by JavaFan (Canon) on Apr 27, 2012 at 09:18 UTC | |
by Marshall (Canon) on Apr 27, 2012 at 11:56 UTC | |
by JavaFan (Canon) on Apr 27, 2012 at 13:10 UTC | |
by SuicideJunkie (Vicar) on Apr 27, 2012 at 13:51 UTC | |
by JavaFan (Canon) on Apr 27, 2012 at 15:46 UTC |