in reply to getting the tail of a string

The correct solution in this case is using basename, since it is a filepath problem. More generally, it's a split() problem. Only most generally is regexp appropriate. Even then, I would avoid POSTMATCH - while the expense is tolerable, it's still high. Better to actually match the string you want, and remember that.

$x =~ m|(?:[^/]/)*/(.*)}; print $1;

--
TTTATCGGTCGTTATATAGATGTTTGCA

Replies are listed 'Best First'.
Re:x2 getting the tail of a string (substr and rindex)
by grinder (Bishop) on Jun 10, 2003 at 17:11 UTC

    I wouldn't say that split and or regexp are the most general ways of attacking this problem. The most general (read: efficient) way would be to find the character with rindex and extract the end of the string with substr. The following does the trick:

      my $ext = substr($x, rindex($x,'.')+1);

    This happens to rely on a side-effect of rindex, which is that if the character sought for is not found in the string, -1 is returned. Add 1 (pedant note: length('.') in fact), and instead of stepping over the sought character, it brings -1 up to 0, and hence your substr will hand you back the whole string. This can be construed as a feature :)

    But yes, File::Basename is the way to go in this particular case.

    update: weird transcription error spotted by sauoq: I had -rindex instead of rindex. Curiously enough, that's exactly what I had when I checked the snippet out on the command line. That'll teach me not to cut and paste...

    _____________________________________________
    Come to YAPC::Europe 2003 in Paris, 23-25 July 2003.