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

Hi,

got a url: http://www.my-website.be/gif/wwwtitle.gif

want the last part www.title.gif in a var.

I got:
where $img is the full url

$naam = substr($img,rindex($img,"/") + 1);
are there better methods ???

Replies are listed 'Best First'.
Re: Rexgrep
by btrott (Parson) on Apr 20, 2000 at 19:51 UTC
    There are ways to do this using a regular expression. For example:
    if ($img =~ m#.*/(.*)#) { $name = $1; } else { # didn't match! }
    This works because the first .* is greedy and matches up until the last "/" in $img; then we keep the rest.

    However, solutions using substr and index (or rindex) are often faster than regexps in cases like this, and this is no exception, at least with the regexp above:

    use Benchmark; timethese(1000000, { 'regexp' => sub { my($file) = $img =~ m#.*/(.*)# }, 'substr' => sub { my $file = substr $img, rindex($img, '/') + 1 } });
    gives
    Benchmark: timing 1000000 iterations of regexp, substr... regexp: 11 secs (11.18 usr 0.00 sys = 11.18 cpu) substr: 4 secs ( 5.67 usr 0.00 sys = 5.67 cpu)
Re: Rexgrep
by ChOas (Curate) on Apr 20, 2000 at 19:56 UTC
    Uuuuhm dunno, I usualy use :
    $Naam=(split /\//,$img)[-1];<BR>
    Dunno if the split is expensive...
      Looks like it's slightly faster than the regexp:
      Benchmark: timing 1000000 iterations of regexp, split, substr... regexp: 16 wallclock secs (16.27 usr + 0.03 sys = 16.30 CPU) split: 17 wallclock secs (15.88 usr + 0.00 sys = 15.88 CPU) substr: 6 wallclock secs ( 5.32 usr + 0.00 sys = 5.32 CPU)
      Hmm, substr is the winner in a big big way. Makes sense. What if we anchor the regexp?         'regexp' => sub { my($file) = $img =~ m#.*/(.*)$# } gives us
      regexp: 17 wallclock secs (16.15 usr + 0.00 sys = 16.15 CPU) split: 15 wallclock secs (15.93 usr + 0.00 sys = 15.93 CPU) substr: 5 wallclock secs ( 5.37 usr + 0.00 sys = 5.37 CPU)
      Nothing else I tried improved things.
Re: Rexgrep
by snowcrash (Friar) on Apr 20, 2000 at 20:00 UTC
    #!/usr/bin/perl -w $_ = "http://www.my-website.be/gif/wwwtitle.gif"; print /\/([^\/]+)$/;
    i'm sure there are better ways but i aint no regexp guru yet ;)
    sc
RE: Rexgrep
by guaguanco (Acolyte) on Apr 20, 2000 at 20:02 UTC
    my $var = "http://www.my-website.be/gif/wwwtitle.gif"; $var =~ s(^.*/)(); print "$var\n";
Re: Rexgrep
by perlmonkey (Hermit) on Apr 20, 2000 at 23:48 UTC
    The 'funny symbols' also tend to be the fastest. It was
    the fastest m// routine, but I found it strange that a s///
    was still faster.

    As mentioned the substr is probably the best anyone can do
    for performance, but for readability I would probably go
    with the replace routine.
    use Benchmark; $img = "http://www.my-website.be/gif/wwwtitle.gif"; timethese(1000000, { 'regexp1' => sub { my($file) = $img =~ m#.*/(.*)# }, 'regexp2' => sub { $img =~ m#[^/]+$#; my $file=$& }, 'regexp3' => sub { $img =~ m#.*/#; my $file=$' }, 'replace' => sub { $file=$img; $file =~ s#^.*/## }, 'split' => sub { my($file) = (split /\//, $img)[-1] }, 'substr' => sub { my $file = substr $img, rindex($img, '/') + 1 +} });
    results:
    Benchmark: timing 1000000 iterations of regexp1, regexp2, regexp3, rep +lace, split, substr... regexp1: 26 wallclock secs (20.06 usr + 0.06 sys = 20.12 CPU) regexp2: 25 wallclock secs (20.02 usr + 0.06 sys = 20.08 CPU) regexp3: 17 wallclock secs (14.83 usr + 0.04 sys = 14.87 CPU) replace: 17 wallclock secs (13.05 usr + 0.03 sys = 13.08 CPU) split: 30 wallclock secs (24.56 usr + 0.09 sys = 24.65 CPU) substr: 11 wallclock secs ( 8.46 usr + 0.02 sys = 8.48 CPU)
RE: Rexgrep
by flyfishin (Monk) on Apr 20, 2000 at 20:34 UTC
    How about using the funny symbols just for kicks? $string =~ /.*\//; $page = $'; print "$page\n";
RE: Rexgrep
by Anonymous Monk on Apr 20, 2000 at 19:51 UTC
    Try this:
    my ( $naam ) = ( $url =~ m:/([^/]+)$: ); # This basically grabs everything that's not a / after # the last / and stores it in $naam.
    cameloid@yahoo.co.uk
Re: Rexgrep
by dsb3 (Initiate) on Apr 22, 2000 at 02:37 UTC
    $url = "http://wherever.com/whatever.gif"; $file = `basename $url`;
    not very portable though :-)