Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Only files starting with xxxx

by Cockneyphil (Acolyte)
on May 11, 2002 at 13:52 UTC ( #165875=perlquestion: print w/replies, xml ) Need Help??

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

This is part of a script which (currently) lists all files in a directory.
How can I alter this script to make it include ONLY the filenames which start with xxxxx ?
opendir THEDIR, "$basepath$ARGV[0]" or die "Unable to open directory: +$!"; my @allfiles = readdir THEDIR; closedir THEDIR; for my $file ( sort { $b <=> $a } @allfiles ) { ..... }

Replies are listed 'Best First'.
Re: Only files starting with xxxx
by TheHobbit (Pilgrim) on May 11, 2002 at 14:13 UTC

    Simple enough... Use grep.Replace the line

    @allfiles = readdir THEDIR;
    @allfiles = grep /^xxxxx/,readdir THEDIR;
    And you're done!


    Leo TheHobbit
Re: Only files starting with xxxx
by choocroot (Friar) on May 11, 2002 at 14:17 UTC
    use the grep function :

    @allfiles = grep { m/^xxxx/ } readdir THEDIR;

    And you'll need to modify your $numfiles calculation ...

Re: Only files starting with xxxx
by fundflow (Chaplain) on May 13, 2002 at 13:33 UTC
    This is probably the simplest:
    @thefiles = <xxxx*>;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://165875]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2021-03-01 20:19 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (22 votes). Check out past polls.