For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > map/grep and arrays









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author map/grep and arrays
Tom Allison

2005-08-26, 6:56 pm

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,,
,,,,
telling me how many of these I actually matched...

I can't be that far off, can I?
Manav Mathur

2005-08-26, 6:56 pm


----- Original Message -----
From: "Tom Allison" <tallison@tacocat.net>
To: "beginners perl" <beginners@perl.org>
Sent: Friday, August 26, 2005 4:49 PM
Subject: map/grep and arrays


> 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,,
> ,,,,
> telling me how many of these I actually matched...
>
> I can't be that far off, can I?
>



Try

print join(",", map{$_=s/[\r\n]+//g} @row), "\n";

Manav


John W. Krahn

2005-08-26, 6:56 pm

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,,
> ,,,,
> telling me how many of these I actually matched...
>
> I can't be that far off, can I?


Not too far off.

print join( ',', map { s/[\r\n]+//g; $_ } @row ), "\n";



John
--
use Perl;
program
fulfillment
Xavier Noria

2005-08-26, 6:56 pm

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
Tom Allison

2005-08-26, 6:56 pm


>print join( ',', map { s/[\r\n]+//g; $_ } @row ), "\n";


bingo!

I wasn't doing the ;$_ thingy in map.
Putting the $_ at the end of the block isn't very intuitive, but it's
"like" a 'return $_' call in a function.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com