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
Solved

read and filter directory contents

Posted on 2008-06-22
4
638 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

809 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