in reply to Getting the filename from full path name ?

While File::Basename is the way to go, I thought that your proposed solution was a bit thought provoking. So, I decided to see if I could do it. Here's what I came up with:
$a = "foo/bar/baz"; my $pos = my $save_pos = 0; do { $pos = index($a, "/", $pos + 1 ); $save_pos = $pos if $pos != -1; } while ($pos != -1); print substr($a, $save_pos + 1);
I'm sure that someone can whittle that down by quite a bit, but it was a fun diversion...:)

thor

Replies are listed 'Best First'.
Re: Re: Getting the filename from full path name ?
by duff (Parson) on Apr 15, 2004 at 03:48 UTC
Re: Re: Getting the filename from full path name ?
by BUU (Prior) on Apr 15, 2004 at 03:47 UTC
    m{/([^/]+)$}
    (split/\//)[-1]
Re: Re: Getting the filename from full path name ?
by pbeckingham (Parson) on Apr 15, 2004 at 03:49 UTC

    This is not a real solution - it is not portable. Please use File::Basename. Having said that...

    my $a = "foo/bar/baz"; my ($file) = $a =~ /\/(.*?)$/;
    Update: Thank you Chris, the code should be:
    my ($file) = $a =~ m|/([^/]+)$|;

      Actually something like my ($file) = $path =~ m|([^/\\]+)$| will work on Win32 or *nix and will also not fail to find the filname of 'file.txt'

      cheers

      tachyon

      I believe you mean
      my ($file) = $a =~ m|/([^/]+)$|;
      Your exp matches starting with the first slash

      Chris