Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: File::Random module (Path::Iterator::Rule)

by Anonymous Monk
on Apr 02, 2013 at 10:21 UTC ( [id://1026647]=note: print w/replies, xml ) Need Help??


in reply to File::Random module

From http://search.cpan.org/perldoc/Path::Iterator::Rule#SYNOPSIS, you add whatever checks you need, same algorithm as File::Random, it reads the whole directory tree but only one filename at a time in memory

sub random_file { my @dirs = @_; my $rule = Path::Iterator::Rule->new; # match anything $rule->file->size(">10k"); # add/chain rules # iterator interface my $next = $rule->iter( @dirs ); my $random_file; my $i = 0; while ( my $file = $next->() ) { if( rand $i++ < 1 ){ $random_file = $file; } } return $random_file; }

Replies are listed 'Best First'.
Re^2: File::Random module (Path::Iterator::Rule)
by gautamparimoo (Beadle) on Apr 02, 2013 at 10:40 UTC

    Thnks.. But one question .How is this rand funtion working ie what does the following statement do:

    if( rand $i++ < 1 )

    Sorry , I just got what it was trying to do.

    One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man. -Elbert Hubbard

      If you don't understand something read the documentaiton and make a short example to illustrate what it's doing in order to better understand what's going on.

      my $i=0; my $x = rand $i++; print "$x\n";

      Update: typo

        I understood the working of rand function. It calculate a random number which is checked against the if condition and if passed than the filename is given. But what if I want to specify to generate atleast 50 or 100 random files?

        One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man. -Elbert Hubbard

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1026647]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found