Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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 meditating upon the Monastery: (6)
As of 2016-10-26 04:06 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (333 votes). Check out past polls.