garu has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to append a single character to a string only if it's not there already. Now, if the char is 'c' (for example) I can do something like:
and it works like a charm, printing "blac" whether $str is "bla" or "blac" already.$char = 'c'; $str = 'bla'; $str =~ s/$char?$/$char/; print "$str\n"
However, if I want to append a backslash instead of a simple character:
it does not work as I'd expect. I even tried '\\\\' but this way I get two backslashes (*as I'd expect*)$char = '\\'; $str = 'bla'; $str =~ s/$char?$/$char/; print "$str\n"
What am I doing wrong? I just ran out of ideas... Any help would be greatly appreciated.
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Appending backslash to a string
by eric256 (Parson) on Aug 10, 2007 at 23:41 UTC | |
by wind (Priest) on Aug 11, 2007 at 00:31 UTC | |
by garu (Scribe) on Aug 11, 2007 at 00:56 UTC | |
by ThomMerton2242 (Initiate) on Jun 29, 2020 at 13:37 UTC | |
by haukex (Archbishop) on Jun 29, 2020 at 14:49 UTC | |
by hippo (Archbishop) on Jun 29, 2020 at 15:46 UTC |