in reply to random file selection

If you need to select a subset of a specified size, then you could do something like this. (Note: You probably don't need the glob if your on *nix.

#! perl -slw use strict; use vars qw[$N]; $N ||= 30; die 'No path specified' unless @ARGV; my @files = glob $ARGV[0]; my $r; $r = rand(@files) and @files[$_,$r] = @files[$r,$_] for 0 .. $N; splice @files, $N; print for @files;

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: random file selection
by Anonymous Monk on Jun 11, 2003 at 16:20 UTC
    I get the 'no path specified' error for this program. I ran the program within the directory that contains the files I wish to randomly extract. I believe that this program is closest to what I am wanting.

      Sorry. I guess I should have added a usage line.

      scriptname [-N=number of files] path\*.ext

      Eg. script -N=10 *.pl will print the names of 30 .pl files from the current directory.

      script "c:\My Files\*" will print the names 30 files of any type from the specified directory.

      You might need to play with it a bit to make it work under *nix.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


        I don't think that this works. Is there a simple explanation for it?

        I don't think that this works. Is there a simple explanation for it?
Re: Re: random file selection
by revdiablo (Prior) on Jun 11, 2003 at 22:03 UTC

    Note: You probably don't need the glob if your on *nix.

    Yes, this is true. Instead of my @files = glob $ARGV[0];, it would be my @files = @ARGV; (or one could simply use @ARGV directly). The reason for this is most unix shells expand globs before perl even gets the command line.