in reply to Extract string from rear of string

You can use rindex to find the index of the rightmost character of string, which you could then modify with substr, but what you really want is File::Basename, it's part of the standard distribution.

update: Indeed, Aigherach is right about the the fact that rindex takes a substring, but that is not germane to the discussion. If you're trying to identify the extension, that probably means you're looking for the rightmost dot in a string.

substr( $s, 0, rindex( $s, '.' )); # the part before the dot substr( $s, rindex( $s, '.' ) + 1); # the part after the dot

But that has portability considerations, and as such is dealt with by fileparse from the File::Basename module.

--
g r i n d e r
just another bofh

print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';

Replies are listed 'Best First'.
Re: Re: Extract string from rear of string
by Aighearach (Initiate) on Dec 28, 2001 at 02:46 UTC
    Actually, it returns the position of the rightmost occurrence of a substring rather than a character. Though, you could use it to check for a substring that is only one character.
           rindex STR,SUBSTR,POSITION
           rindex STR,SUBSTR
                   Works just like index() except that it returns the
                   position of the LAST occurrence of SUBSTR in STR.
                   If POSITION is specified, returns the last occur­
                   rence at or before that position.
    

    --
    Snazzy tagline here
Re: Re: Extract string from rear of string
by Anonymous Monk on Dec 28, 2001 at 06:34 UTC
    or
    $ext = (split /\./, $fname)[-1];

    But neither take note of the fact that the file might not have an extention...

      ..or more than one dot... like linux-2.4.17.tar.gz
        Begging your pardon, but the split shown returns the last bit of stuff after the final dot, which I think complies with the rather vague request of getting the extension. In your example, the extension is 'gz' after all. That's the beauty of negative indices, they work from the other end.