LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2012, 02:46 AM   #1
Angel2953
Member
 
Registered: Sep 2005
Location: Poland
Distribution: Debian, Ubuntu
Posts: 38

Rep: Reputation: 16
[perl] array of files: remove specific ext


Hello,

I have a problem cause i have an array of files and dirs:
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",
);
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...
 
Old 01-02-2012, 06:29 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
you can easily iterate over the array and create a new array that only hosts the files like this:

Code:
foreach $file (@files) {
	if ( -f $file ) {
		push(@no_dirs, $file);
	}
}
The new @no_dirs array will not contain any directory.
 
1 members found this post helpful.
Old 01-02-2012, 06:51 PM   #3
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
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;

Last edited by Cedrik; 01-02-2012 at 07:35 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[perl scripting] help with applying array in a specific scenario confusednewbie Programming 7 04-18-2011 12:16 PM
[SOLVED] Bash command to remove all files within a specific folder shayno90 Linux - Newbie 21 10-21-2010 11:55 AM
Remove files that contain a specific string poymode Linux - General 5 02-17-2010 03:01 AM
[perl] copying an array element into another array s0l1dsnak3123 Programming 2 05-17-2008 01:47 AM
Remove ALL files from specific Directory bianchi Programming 9 11-23-2005 11:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration