Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i read the file name in a directory with opendir and readder, example:
my $dir = "c:/temp/"; opendir (DIR, $dir) or die "Unable to open dir $dir : $!"; my @files = readdir(DIR) or die "Unable to readdir $dir : $!"; closedir DIR; print "@files <br>";
Now, i would like to read only the file name that begin with xxxx

Can you help me with an example ??

Thank you Claudia

broquaint: added code tags and formatting

Title edit by tye

Replies are listed 'Best First'.
Re: readdir
by CubicSpline (Friar) on Oct 29, 2002 at 18:52 UTC
    You have a couple of options.

    First you could use your existing code and "grep" out the ones you want. Example:

    my @files = grep(/xxxx\w+/,readdir(DIR));

    Second, you could use a glob function to grab them. Example:

    my $dir = "c:\\temp"; my @files = <$dir\\xxxx*.*>;

    ~CubicSpline
    "No one tosses a Dwarf!"

Re: readdir
by Speedy (Monk) on Oct 29, 2002 at 19:49 UTC

    Perldoc is your friend here.

    From your $dir path, looks like you are on windows. So go to a DOS prompt, enter:

    perldoc -f readdir

    and you will see by happy coincidence as the example in the last paragraph an answer to your question:

    -------------------------

    If you're planning to filetest the return values out of a "readdir", you'd better prepend the directory in question. Otherwise, because we didn't "chdir" there, it would have been testing the wrong file.

    opendir(DIR, $some_dir) || die "can't opendir some_dir: $!"; @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR); closedir DIR;

    ------------

    Just replace the ^\. with ^xxxx and your job is done, like:

    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!"; @xxxx = grep { /^xxxx/ && -f "$some_dir/$_" } readdir(DIR); closedir DIR; print "@xxxx";

    Live in the moment

Re: readdir
by Enlil (Parson) on Oct 29, 2002 at 18:53 UTC
    you could change the line starting with  my @files to something like:
    my @files = grep {/^xxxx/} readdir(DIR);

    -enlil

Re: readdir
by belg4mit (Prior) on Oct 29, 2002 at 18:51 UTC
    You have to read all the filenames. However you can filter out the files whose names you are interested in. grep would be a good choice for this.

    --
    perl -wpe "s/\b;([mnst])/'$1/g"