In article <d14iaq$d4i$>,
Shashank Khanvilkar <> wrote:
:Suppose I have a variable defined as:
:$a = "This is a good example";
:Note that there are multiple blanks between the words.
:If i want to convert the above string to
:$b = "This is a good example"
:Is there any shortcut to do the above.
($b = $a) =~ s/ +/ /g;
Or if you want to generalize this a bit,
($b = $a) =~ s/\s+/ /g;
\s matches whitespace (includes tabs and various non-ASCII whitespace.)
The + modifier means "one or more of the previous group".
The trailing g means "global replace" -- that is, do for all occurances
in the input string.
The other trick is the ($b = $a). If you were to use
$b = $a =~ s/\s+/ /g;
then $b would be assigned the result of the =~ operation; in a scalar
context, =~ with a substitution returns the number of substitutions made,
so $b would end up as a count instead of as the new string. The
($b = $a) part causes the assignment to be done first, and then the =~
acts upon the string so created.
--
Warning: potentially contains traces of nuts.
|