Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

apply a regular expression to glob'd filenames

by flieckster (Scribe)
on Oct 01, 2019 at 20:05 UTC ( #11106920=perlquestion: print w/replies, xml ) Need Help??

flieckster has asked for the wisdom of the Perl Monks concerning the following question:

hello all, i'd like to apply a regular expression after i glob some filenames from a directory. the files in question are .tif files, and i'd like to remove the file extensions from them to use later. I tried grep with a regex and it isn't returning what i'd like.
chdir( $wetrasnfer) or mail ($to = $internaldl, $from = $internaldl, $ +subject= "wetransfer $! $0", $body ="ERROR $wetrasnfer $! $0" ); (@wetransfer) = glob '*.tif'; @wetrasnfer_fix = grep /([0-9])*_([0-9]*)/, @wetransfer; print join("\n", @wetrasnfer_fix), "\n";
the code above results in returning the full filename.
00204597_957.tif 00271558_148.tif 00271558_585.tif
ideally i'd want above returned with the .tif missing.

Replies are listed 'Best First'.
Re: apply a regular expression to glob'd filenames
by choroba (Archbishop) on Oct 01, 2019 at 20:26 UTC
    grep is for filtering, map is for transforming.
    my @wetrasnfer_fix = map /([0-9]*_[0-9]*)/, @wetransfer;

    With 5.14+, you can also use the "return" substitution modifier:

    my @wetrasnfer_fix = map s/\.tif$//r, @wetransfer;

    Or, you can use split:

    my @wetrasnfer_fix = map +(split /\./)[0], @wetransfer;

    Note from stevieb: You should also handle the case when not only the chdir fails, but the mail fails, too.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      ahh, thank you. the map worked perfectly!
Re: apply a regular expression to glob'd filenames
by Fletch (Bishop) on Oct 01, 2019 at 20:28 UTC

    grep returns items from a list for which the predicate returns true. You want to map the list of matching filenames to remove the extension.

    @wetransfer_fix = map { (my $t = $_) =~ s/\.tif//; $t } grep # b +efore...

    Also someone else mentioned in the chatterbox that you might want to change how you're doing error handling on your initial chdir and die so you don't continue trying to run in the wrong directory.

    unless( chdir( $wetrasnfer ) ) { my $chdir_error = $!; ## In case mail() clobbers old value mail( ... blah ... ); ## send email notification die qq{Couldn't send error mail: $chdir_error\n}; }

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: apply a regular expression to glob'd filenames
by 1nickt (Canon) on Oct 01, 2019 at 20:58 UTC

    Hi, this is not a job for a regexp. Use the core module File::Basename to handle stripping extensions, or even better use Path::Tiny or Path::Iterator::Rule to find the files and get your desired output.

    Here's one way:

    $ ls /tmp/11106920/
    123456.tif 123_456.tif 666_666.jpeg 678_910.tif
    $ perl -Mstrict -MPath::Tiny -wE 'path("/tmp/11106920")->visit( sub { +say $_->basename(".tif") if /[0-9]+_[0-9]+\.tif/ } );'
    678_910 123_456

    Hope this helps!

    The way forward always starts with a minimal test.
Re: apply a regular expression to glob'd filenames
by hippo (Bishop) on Oct 01, 2019 at 21:06 UTC

    TIMTOWTDI. Here's an approach using a for loop.

    use strict; use warnings; use Test::More tests => 1; my @have = qw/00204597_957.tif 00271558_148.tif 00271558_585.tif/; my @want = qw/00204597_957 00271558_148 00271558_585/; s/\..*// for @have; is_deeply \@have, \@want;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11106920]
Approved by stevieb
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-05-22 13:35 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (80 votes). Check out past polls.