Newbie-question: Regexp to match the file extension

F

florian.haas

Hi !
I am using File::find to recursively go through a certain directory.
I have a String containing the file extensions that i am looking for
(separated by commas). I split that String up and try to match the
filenames to the extension.
Somehow i am unable to get it to work.
(My aim is to only get Files with the extensions .c, .d or .e)

$extensions = ".c,.d ,.e"

sub foundfile {
my $abortboolean= "true";
return unless -f $_; # Only Files are interesting
my @extensionarray = split(/,[' ']?/, $extension);
my $array_element ;
foreach $array_element(@extensionarray)
{if (/$$array_element/i){$abortboolean="false";}}
if ($abortboolean eq "true"){return;}
print "File $_ is ok\n";
}
Any help is apreciated.

By the Way: Do you know a good Regexp-Tutorial ?
 
J

Jürgen Exner

Hi !
I am using File::find to recursively go through a certain directory.
I have a String containing the file extensions that i am looking for
(separated by commas). I split that String up and try to match the
filenames to the extension.

I would use File::Basename to isolate the extension rather than rolling your
own solution using split().
Then a simple comparison with the wanted extension should to the job.
Somehow i am unable to get it to work.
(My aim is to only get Files with the extensions .c, .d or .e)

$extensions = ".c,.d ,.e"

You define $extensions, but you don't use that variable anywhere in your
code. Are you using strictures and warnings?

It might be better to write this as
my @extensions = qw/.c .d .e/;
sub foundfile {
my $abortboolean= "true";
return unless -f $_; # Only Files are interesting
my @extensionarray = split(/,[' ']?/, $extension);

Where does $extension come from? It is undeclared at this point.
my $array_element ;
foreach $array_element(@extensionarray)
{if (/$$array_element/i){$abortboolean="false";}}

What is this supposed to do? Do you really mean to use symbolic references,
i.e. are you really trying to match $_ against the content of the variables
$.c, $.d and $.e?
I doubt those variables are defined.
if ($abortboolean eq "true"){return;}

At the beginning of the sub{} you defined $abortvalue to be 'true' and you
didn't change the value anywhere, so this will aways succeed.
print "File $_ is ok\n";

and you will never see this print().
}
Any help is apreciated.

I would dump the whole sub and go straight to the wanted() function,
something along the line of (untested!):

use File::Basename;

%extensions = qw/c undef d undef e undef/;
# predefine hash with keys c, d, and e
# actual value is irrelevant
sub wanted {
-f and exists $extensions{lc(fileparse($_)[2])};
}

It might even be that you can use @suffixlist to your advantage and that you
don't even need %extensions.
By the Way: Do you know a good Regexp-Tutorial ?

Did you check "perldoc perlretut"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top