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:
random file selection

 



oldtimer
Deleted

Mar 30, 2001, 4:26 PM

Post #1 of 2 (162 views)
random file selection Can't Post

I have a dir with lots of text files, the number of files dynamically changing hour to hour.

What I need is a routine to choose 1 file at random and set this name in a variable for other use.



Jean
User


Apr 1, 2001, 8:18 AM

Post #2 of 2 (154 views)
Re: random file selection [In reply to] Can't Post

Try this:


Code
my $dir = "/usr/local/test"; 
my $file;
my @files;
my $result;

opendir(DIR, $dir) or die "Unable to open dir $dir";
while ($file = readdir(DIR)) {
push(@files, $file) if (-f $file);
}
closedir(DIR);

$result = $files[int(rand $#files)];
print "$result\n";

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com

 
 


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

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