Newbie-question: Regexp to match the file extension

Discussion in 'Perl Misc' started by florian.haas@gmail.com, Sep 22, 2005.

  1. Guest

    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 ?
     
    , Sep 22, 2005
    #1
    1. Advertising

  2. 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"?
     
    Jürgen Exner, Sep 22, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikel Lindsaar
    Replies:
    0
    Views:
    546
    Mikel Lindsaar
    Mar 31, 2008
  2. Old Echo
    Replies:
    1
    Views:
    200
    Adam Shelly
    Sep 4, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    407
    7stud --
    Aug 21, 2009
  4. Kenneth Brun Nielsen

    Newbie: Simple conditional on regexp match

    Kenneth Brun Nielsen, Aug 6, 2008, in forum: Perl Misc
    Replies:
    3
    Views:
    146
  5. Uldis  Bojars
    Replies:
    2
    Views:
    214
    Janwillem Borleffs
    Dec 17, 2006
Loading...

Share This Page