[EMAIL PROTECTED] wrote:

> >Replace
> >
> >  EVIL: map { some;block;of;code;that;changes;$_ } @some_array;
> >
> >with
> >
> >  GOOD: for (@some_array) { some;block;of;code;that;changes;$_ }
>
> I guess I don't get it.  Map returns a value and I ignore it; so what?
> What side effects does this have?  Which one's faster?  I like to avoid
> obvious loops when possible because I perceive them as slow, so I often use
> map, and rarely (actually, to date, never) care about the return value.  If
> there's no savings then I guess it doesn't matter.  But what makes it bad?
>
> Peter

Hi Peter,

The number of lines in the ocde doesn't necessarily track to the amount of processing. 
 Think about it a bit.  You can assign the output of the function to a list.  That 
means that, somewhere in the innards of the function, it is building a list of the 
returned values from the BLOCK|EXPR parameter.  Whether you use it or not, this 
overhead is there.  It is therefore unlikely to be faster in acting on the source 
array.

Beyond that, why obscure your code?  If you want to do something for each element of 
an array, tell the interpeter to do this for each.  If you want an array that 
represents a mapping of the elements of an array as affected by some set of 
operations, then map the array to a function, block, or expression. I'd suggest using 
it with functions that take thier parameter by value.  I've included an example below.

Joseph

Somewhat wasteful [for reasons described above]:

#!/usr/bin/perl -w

use strict;

my @num_array = (0,3,4.5,1,2,17);
map (increment(), @num_array);
print @num_array;

sub increment {
  $_++;
  $_ .= "\n";
}

Output:
E:\d_drive\perlStuff\guests>maptest.pl
1
4
5.5
2
3
18
a 123
C joyjoyjoy
b fff

Although there is some overhead, no great damage is done in terms of the payload.  
Here you wanted to modify the array, and that is the result you achieved.

Used safely to generate a mapping:

#!/usr/bin/perl -w

use strict;

my @num_array = (0,3,4.5,1,2,17);
my @incremented_nums = map (increment(), @num_array);
print @incremented_nums;
print "\n";
print @num_array;

sub increment {
  my $number = $_;  #  Note use of local scope. We don't want to touch the $_.
  $number++;
  return $number .= "\n";
}

Output:
E:\d_drive\perlStuff\guests>maptest.pl
1                     #  Good.  we have our payload in the @incremented_nums array
4
5.5
2
3
18

034.51217    #  Even better.  Original data remains uncorrupted.
By coying the dsefault variabvle $_ to the local variable $number within increment(), 
this approach insulates the data in the original array from the processing used to 
generate the mapping.

Downright disastrous:

#!/usr/bin/perl -w

use strict;

my @num_array = (0,3,4.5,1,2,17);
my @incremented_nums = map (increment(), @num_array);
print @incremented_nums;
print "\n";
print @num_array;

sub increment {
  $_++;
  $_ .= "\n";
}

Output:
E:\d_drive\perlStuff\guests>maptest.pl
1           # Good!  This is the product we want in incremented_array
4
5.5
2
3
18

1             # Whoops!  @num_array has been accidentally incremented as a side effect!
4
5.5
2
3
18
Here you would have toasted the original array, which presumably was not your 
intention.  If it was, you could have used a for loop.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to