On Aug 26, 2005, at 13:19, Tom Allison wrote:
I keep getting hung up on what I think would be a simple problem.
I was to do approximately this:
Given an array of "stuff" print them out
print join(",", @row),"\n";
works great until your array elements contain dumb characters like
\n\r somewhere in the string (not always at the end!).
Initially I thought I could do:
print join(",", map(s/[\r\n]+//g, @row)), "\n";
print join(",", map{s/[\r\n]+//g} @row), "\n";
but I get output like:
,,2,,
,,,,
This is a common error: there map builds a list whose elements are
respectively the _result_ of applying s/// to each element of @row.
Now, it is natural (until you think twice) to associate the result of
s/// with the new $_ and expect map to build a list with those
resulting $_s. But s/// does _not_ evaluate to the new $_, the
modified $_ is a side-effect of s///. On the contrary, s/// itself
returns something:
Searches a string for a pattern, and if found, replaces that
pattern with the replacement text and returns the number of
substitutions made. Otherwise it returns false (specifically,
the empty string).
And that's what you are getting, see what happens?
A possible fix is:
s/[\r\n]+//g for @row; # sanitizes @row in place
join(",", @row);
-- fxn
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>