in reply to Re: Using s///e and it just doesn't feel right
in thread Using s///e and it just doesn't feel right
map should not be used in a void context, that's why foreach is there.
map { print "$_\n" } (@array);
foreach (@array) { print "$_\n" }
These are functionally the same, but map generates a return value that can be assigned to another array, and foreach does not, therefore map is not appropriate. Instead it should be used for its return value:
@squares = map { $_ * $_ } (@array);
The above code with foreach would have to be:
foreach (@array) { push @squares, $_ * $_ }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Using s///e and it just doesn't feel right
by perlguy (Deacon) on Jun 20, 2003 at 20:13 UTC | |
|
Re: Re: Re: Using s///e and it just doesn't feel right
by waswas-fng (Curate) on Jun 20, 2003 at 16:28 UTC | |
by bunnyman (Hermit) on Jun 20, 2003 at 16:44 UTC | |
by antirice (Priest) on Jun 20, 2003 at 16:58 UTC | |
by dash2 (Hermit) on Jun 20, 2003 at 17:07 UTC | |
by jsprat (Curate) on Jun 20, 2003 at 17:45 UTC | |
| |
by antirice (Priest) on Jun 20, 2003 at 17:26 UTC | |
|
Re: Re: Re: Using s///e and it just doesn't feel right
by kral (Monk) on Jun 23, 2003 at 08:44 UTC |