[SOLVED] [perl] array of files: remove specific ext
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
first what i need is to remove all items that are dirs and then group rest of the files into their own arrays. So i though about function that takes 2 params: 1. array to scan, 2. ext to filter and returns filtered array... And now how to implement that. I mean how to iterate throught array and remove some elements based on a filer...
You can also use grep perl function to select files with specific extension, like:
Code:
my @files = (
"/path/to/file/file_1.ext1",
"/path/to/file/file_2.ext1",
"/path/to/file/file_3.ext1",
"/path/to/file/file_4.ext2",
"/path/to/dir1",
"/path/to/file/file_5.ext2",
"/path/to/file/file_6.ext1",
"/path/to/dir2",
"/path/to/file/file_7.ext3",
"/path/to/dir3",
"/path/to/file/file_8.ext4",
"/path/to/file/file_9.ext5",
"/path/to/dir4",
);
# put all files with .ext1 extension in @ext1 array
my @ext1 = grep {/\.ext1$/} @files;
# see the result
print "$_\n" for @ext1;
# or remove all files with .ext1 extension
my @noext1 = grep {! /\.ext1$/} @files;
# see the result
print "$_\n" for @noext1;
# You can also use grep to remove dirs from list
my @just_files = grep { -f } @files;
# see result
print "$_\n" for @just_files;
# you can also combine more grep...
my @ext1_files = grep { /\.ext1$/ } grep { -f } @files;
# or simply:
my @ext1_files = grep { -f and /\.ext1$/ } @files;
# result should look like the same as with just greping .ext1 extension
# but the files existence is tested
print "$_\n" for @ext1_files;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.