Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie-question: Regexp to match the file extension

Reply
Thread Tools

Newbie-question: Regexp to match the file extension

 
 
florian.haas@gmail.com
Guest
Posts: n/a
 
      09-22-2005
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 ?

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-22-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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"?


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM
How to convert a .txt file extension to a .xls file extension? Steve ASP .Net 3 08-25-2006 05:43 PM



Advertisments