in reply to add a character at the end of a string
I guess there are alternatives, but it works:
# can't be called with a literal string as arg # but that wasn't asked for :) sub appendMissingSlash { return $_[0] .= substr($_[0], -1) eq "/" ? "" : "/"; } my $string = "/tmp/check"; appendMissingSlash($string); print $string,"\n"; __END__ /tmp/check/
Update: I didn't use a regex, as other examples given suggest, because we know the position and the character to check for, using the regex engine seemed overkill to me
regards,
tomte
An intellectual is someone whose mind watches itself.
-- Albert Camus
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: add a character at the end of a string
by jhourcle (Prior) on Dec 13, 2005 at 12:18 UTC | |
by Tomte (Priest) on Dec 13, 2005 at 13:17 UTC |
In Section
Seekers of Perl Wisdom