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 ) { ..... }

Comment on Only files starting with xxxx
Select or Download Code
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 browsing the Monastery: (14)
As of 2015-11-30 15:09 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (774 votes), past polls