Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

read and filter directory contents

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
NorthReptile
Asked:
NorthReptile
  • 2
1 Solution
 
jerzy_szCommented:
You can use regular expression to filter your "files" array.
@files = grep ( /jpg|gif|psd/i, <$in/*>);

Open in new window

0
 
ozoCommented:
@files = <$in/*.{jpg,gif,psd,etc)}>;
0
 
NorthReptileAuthor Commented:
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
 
jerzy_szCommented:
Note, that second solution is case sensitive.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now