Link to home
Start Free TrialLog in
Avatar of NorthReptile
NorthReptile

asked on

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).
Avatar of jerzy_sz
jerzy_sz
Flag of Poland image

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NorthReptile
NorthReptile

ASKER

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.
Note, that second solution is case sensitive.