[TriLUG] Perl substitution problem

William Sutton via TriLUG trilug at trilug.org
Fri Jun 16 13:20:44 EDT 2017


so, if you

my $string2 = 'this "should"*,* work';
$string2 =~ s/"/\\"/g;
print "$string2\n";


(notice the quotes in the print statement), then you get:

this \"should\"*,* work

which is what you wanted.

William Sutton

On Fri, 16 Jun 2017, William Sutton via TriLUG wrote:

> use strict not required for my/our.  he did something else when he pasted 
> his code.
>
> Also, HTH does
>
> print $string1 \n;
>
>
> work in Perl?  It needs to be
>
> print "$string1 \n";
>
> otherwise, I'm looking at your question
>
> William Sutton (Perl expert)
>
> On Fri, 16 Jun 2017, Brian via TriLUG wrote:
>
>> You need a
>>
>> use strict;
>>
>> for "my" to work, which Wes omitted probably for the sake of brevity.
>>
>> -B
>>
>>
>> On 06/16/2017 01:07 PM, karl flores via TriLUG wrote:
>>> Error: Parse error on line 1:
>>> my $string2 = 'this
>>> ^
>>> Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 
>> 'undefined'
>>>
>>>
>>> On Fri, Jun 16, 2017 at 12:57 PM, Wes Garrison via TriLUG 
>> <trilug at trilug.org
>>>> wrote:
>>>
>>>> My Google-fu is not good enough to solve this simple problem.
>>>>
>>>> I am building some simple JSON structures by hand in Perl, and I need to
>>>> escape double-quotes.
>>>>
>>>> my $string1 = 'this "should" work';
>>>> $string1 =~ s/"/\\"/g;
>>>> print $string1 \n;
>>>> this \"should\" work
>>>>
>>>> ^ Yay!  it substituted correctly for both double-quotes!
>>>>
>>>> my $string2 = 'this "should"*,* work';
>>>> $string2 =~ s/"/\\"/g;
>>>> print $string2 \n;
>>>> this \"should" work
>>>>
>>>> ^ the comma after the second double-quote broke the substitution. (and it
>>>> breaks my JSON)
>>>>
>>>> Why?  What the heck am I missing?
>>>>
>>>> -Wes
>>>> _________________________________
>>>> Wesley S. Garrison
>>>> Network Engineer
>>>> Xitech Communications, Inc.
>>>> phone:  (919) 260-0803
>>>> fax:       (919) 932-5051
>>>> __________________________________
>>>> "Lead us not into temptation, but deliver us from email."
>>>> --
>>>> This message was sent to: karl flores <karl.americorp at gmail.com>
>>>> To unsubscribe, send a blank message to trilug-leave at trilug.org from that
>>>> address.
>>>> TriLUG mailing list : https://www.trilug.org/mailman/listinfo/trilug
>>>> Unsubscribe or edit options on the web  : https://www.trilug.org/
>>>> mailman/options/trilug/karl.americorp%40gmail.com
>>>> Welcome to TriLUG: http://trilug.org/welcome
>>>
>>>
>>>
>>>
>>
>> -- 
>> This message was sent to: William <william at trilug.org>
>> To unsubscribe, send a blank message to trilug-leave at trilug.org from that 
>> address.
>> TriLUG mailing list : https://www.trilug.org/mailman/listinfo/trilug
>> Unsubscribe or edit options on the web	: 
>> https://www.trilug.org/mailman/options/trilug/william%40trilug.org
>> Welcome to TriLUG: http://trilug.org/welcome
> -- 
> This message was sent to: William <william at trilug.org>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that 
> address.
> TriLUG mailing list : https://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web	: 
> https://www.trilug.org/mailman/options/trilug/william%40trilug.org
> Welcome to TriLUG: http://trilug.org/welcome


More information about the TriLUG mailing list