Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, for the best part of a hour, i'm attempting to replace dollar instance in a string. However, the substitution doesn't work. Internet search hasn't helped with what i possibly could be missing in the complete code below. Any pointers ? Thanks in advance.
#!/usr/bin/perl -W use strict; use warnings 'FATAL'; use diagnostics; use utf8; use 5.008_008; my $s1; my $s2; $s1 = 'This is B & B and T & D.'; $s1 =~ s/\\\$//g; printf("%s\n", $s1); $s2 = 'This is B & B and T & D.'; $s2 =~ s/\x24//g; printf("%s\n", $s2); 1;

Replies are listed 'Best First'.
Re: replace multiple dollar instances ($) in a string
by Eily (Monsignor) on Sep 10, 2018 at 08:33 UTC

    If you're just trying to delete single characters, a more appropriate tool would be tr with the /d option (delete). $s1 =~ tr/$&//d;

    Edit: tr is a slightly better tool in that case because, beyond being faster, characters in the left part don't have a special meanings, so $ and & just mean $ and &; not the $& variable, or the end of the string (for $). There's no escaping needed.

    Corrected typo, it's /d not /g. Thanks AnomalousMonk & kcott

Re: replace multiple dollar instances ($) in a string
by Corion (Patriarch) on Sep 10, 2018 at 08:28 UTC

    Where in your data are the dollars that you're trying to replace?

    If you're trying to replace ampersands (&), then you should do so in your regular expression too:

    s/&//g;