in reply to Using substr to remove characters

after all, i still don't understand, why you're usiińg substr() at all - instead of a regex:
#!/usr/bin/perl -w use strict; my $str = "This_is_a_song_.mp3"; $str =~ s/_(\.mp3)$/$1/g; print "$str\n";
another way might be:
use strict; my $str = "This_is_a_song_.mp3"; my ($pre, $post) = split /\./, $str; chop $pre; $str = "$pre.$post"; print "$str\n";
... and so on, and so on... :D

language is a virus from outer space.

Replies are listed 'Best First'.
Re^2: Using substr to remove characters
by JediWizard (Deacon) on May 09, 2005 at 13:34 UTC

    i still don't understand, why you're usiińg substr() at all - instead of a regex

    Speed

    $str =~ s/_(\.mp3)$/$1/g; # Or Much faster with the same result: substr($str, -5, 1, "") if(substr($str, -5, 1) eq '_');

    If you do not need the power of metacharacters which a regex offers, don't use a regex. Using a regex for this opperation is like using a sledge hammer to pound in a small nail.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol