Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Newbie-question: Regexp to match the file extension (http://www.velocityreviews.com/forums/t894363-newbie-question-regexp-to-match-the-file-extension.html)

florian.haas@gmail.com 09-22-2005 08:05 AM

Newbie-question: Regexp to match the file extension
 
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ürgen Exner 09-22-2005 09:31 AM

Re: Newbie-question: Regexp to match the file extension
 
florian.haas@gmail.com wrote:
> 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"?




All times are GMT. The time now is 05:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.