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

Monks,

I'm looping through an array of file pathnames passed as a param from another program. I'm trying to get a list of just the file names at the end of the filepath. The paths can be any length long. What I'm looking for is the specific patten matching syntax to get this going.

Sample input
/blah/blah/blah/blah/file1.pl
/blah/blah/file2.pl
/blah/blah/blah/blah/blah/blah/blah/file3.pl

Desired output
file1.pl
file2.pl
file3.pl

Any words of wisdom from the ones that know?

Replies are listed 'Best First'.
Re: Truncating Filename string
by Zaxo (Archbishop) on Jan 28, 2004 at 01:24 UTC

    File::Basename, it's in the core.

    use File::Basename; my @suffixes = qw/.pl .plx .pm/; while (<>) { print basename( $_, @suffixes), $/; }

    After Compline,
    Zaxo

Re: Truncating Filename string
by davido (Cardinal) on Jan 28, 2004 at 05:38 UTC
    I may as well take a swing at the regexp approach though File::Basename is probably a more robust solution.

    Here goes...

    my $string = '/whatever/filepath/and/name.dat'; my $filename; if ( $string =~ m#/([^/]+)$# ) { $filename = $1; print $filename, "\n"; }

    The regexp translated: Start by using 'm##' as the regexp delimiter instead of the more traditional m//, so that you don't have to escape the '/' character within the expresssion. Next, match the last '/' character in the string, then capture all of the rest of the characters that are not '/' through the end of the string.

    This assumes that you're on an OS where path levels are delimited with '/'. You'll have to modify it if '\' is your delimiter of choice. This also assumes that there is at least one '/' in the path. ...so many assumptions, the module is probably a safer way of handling it.


    Dave

Re: Truncating Filename string
by Roger (Parson) on Jan 28, 2004 at 01:15 UTC
    my $fullpath = "/blah/blah/blah/blah/file1.pl"; my ($filename) = $fullpath =~ m!([^/]+$)!; print "$filename\n";

Re: Truncating Filename string
by borisz (Canon) on Jan 28, 2004 at 01:16 UTC
    #!/usr/bin/perl m!([^/]*)$! and print "$1" while (<DATA>); __DATA__ /blah/blah/blah/blah/file1.pl /blah/blah/file2.pl /blah/blah/blah/blah/blah/blah/blah/file3.pl
    Boris
Re: Truncating Filename string
by b10m (Vicar) on Jan 28, 2004 at 01:15 UTC

    You could try something like this:

    while(<DATA>) { if($_ =~ m/\/([\w\d\.]+)$/) { print "$1\n"; } } __DATA__ /blah/blah/blah/blah/file1.pl /blah/blah/file2.pl /blah/blah/blah/blah/blah/blah/blah/file3.pl

    This will result in the desired output.

    --
    b10m

    All code is usually tested, but rarely trusted.
      All code is usually tested, but rarely trusted.

      You said it (so I'm just repeating what you said ;^).

      m/\/([\w\d\.]+)$/
      Remember that "\w" represents alphanumerics and underscore -- no need to add "\d" to a character class when "\w" is already there.

      This will result in the desired output.

      ... for the three examples provided, but not for file names that contain dashes, plus-signs, tildes, colons, ... Better not to try to match the file name content (since you don't need to).