Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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
Re: Only files starting with xxxx
by TheHobbit (Pilgrim) on May 11, 2002 at 14:13 UTC

    Hi,
    Simple enough... Use grep.Replace the line

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

    Cheers


    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-07-22 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (106 votes), past polls