in reply to Re^2: Formatting quotes in a String
in thread Formatting quotes in a String
Hi pavan474,
Regarding choroba's code with an attempt to supplement it with a text explanation:
The ($string =~ tr/"//) portion provides a simple count of the number of double quotes contained within the string, it doesn't modify the string. He uses the modulo operator to divide this count by 2 and keep only the remainder, ($string =~ tr/"//) % 2. Due to the math, that remainder can only be 0 (if there were an even number of double quotes) or 1 (if there were an odd number of double quotes). Then he concatenates a double quote on to the end of the string, $string .= '"', but only if that remainder was 1 (an odd number of double quotes).
UPDATE:
Also consider the following if you wanted to check what the ($string =~ tr/"//) portion gives before and after the concatenation of the double quote on to your original string.
$string = '"testing the data available'; print scalar ($string =~ tr/"//), "\n"; $string .= '"' if ($string =~ tr/"//) % 2; print ($string =~ tr/"//);
|
|---|