Solved

read and filter directory contents

Posted on 2008-06-22
4
649 Views
Last Modified: 2008-06-23
I use the following code to read a list of contents from a directory

[code]
@files = <$in/*>;
      foreach (@files) {
        }
[/code]

I know it's easy to filter one file type by placing the extension after the * but I would like to filter a list of file types but cannot figure out how to do it.

i.e. filter a list of picture files (jpg, gif, psd, etc).
0
Comment
Question by:NorthReptile
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:jerzy_sz
ID: 21840569
You can use regular expression to filter your "files" array.
@files = grep ( /jpg|gif|psd/i, <$in/*>);

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 21840941
@files = <$in/*.{jpg,gif,psd,etc)}>;
0
 

Author Comment

by:NorthReptile
ID: 21841152
Thanks, Ozo's solution was exactly what I was after. I am sure the grep works just as fine too but i prefer the syntax of the other.

thanks to you both.
0
 
LVL 1

Expert Comment

by:jerzy_sz
ID: 21847441
Note, that second solution is case sensitive.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl script to parse log and delete the file 17 183
perl CPAN issue 3 130
syslog unix file 20 80
this is a continuation of my array problem in perl cemetery problem 2 14
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question