DSP DSP - 3 years ago 265
Perl Question

Why are my double quotes are missing in the output?

I tried to convert strings from lower case to upper case. While I achieved that, I found my double quotes were removed from the output. I want to be clear why the substitution fails here.

Perl code:


use strict;
use warnings;

my @ar = <DATA>;
my $str = join '', @ar;

#print $str;

if ( $str =~ s/\"([^"]*)\"/uc($1)/eg ) {
print $str;
output = " as (10) print "it if achieved. print" # This is comment.NUMBERS"

Obtained output:

output = AS (10) PRINT it if achieved. print # THIS IS COMMENT.NUMBERS

Expected output:

output = " AS (10) PRINT "it if achieved. print" # THIS IS COMMENT.NUMBERS"

Answer Source

s/\"([^"]*)\"/uc($1)/eg - $1 represents match inside brackets (). You have include double quotes in match to be replaced but outside brackets.

Possible fix: s/(\"[^"]*\")/uc($1)/eg

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download