Errin Larsen wrote:
On Fri, 10 Sep 2004 12:44:51 -0400, David Greenberg
<[EMAIL PROTECTED]> wrote:

foreach( @ARGV ) {
     open IN, $_ or die "Couldn't open $_: $!\n";
     chomp( my @data = <IN> );
     close IN;
     foreach( @data ) { s/\s+//g; }
     foreach( 0..$#data ) { $results[$_] .= $data[$_]; }
}

This is a little shorter and saves on iterations: for my $file (@ARGV) { open IN, $file or die "Couldn't open $file: $!\n"; @results = map { my $line = $_; chomp $line; $line =~ s/\s+//g; $line } (<IN>); close IN; }

Can you throw the 'chomp' in the assignment in that 'map' statement?

No.

Then, can you also throw in the substitution in the mix?

No.

> like this:
@results = map{ my $line = chomp( s/\s+//g ); } ( <IN> );

The substitution operator returns the number of substitutions performed or false if there were no substitutions. chomp() returns the number of chomps performed. map returns the value of the last statement in the code block.


So, assuming s/\s+//g returns a number and $/ hasn't been changed, chomp will assign 0 to $line which map will return for every line from IN.


And if so, why not this:

@results = map{ chomp( s/\s+//g ) } ( <IN> );

Again, map will return 0 for every line from IN.

Also, the parentheses around the readline operator are redundant as map forces list context.


As long as we're playing Perl-Golf!!

I truly don't understand what 'map' is doing.  Can you explain it to
me?  I have tried to read perldoc -f map but it's a little weird and
hard to follow!

map takes a list, modifies each element, and returns the modified list.



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to