Rob Dixon wrote:
Weide wrote:
Can anyone tell me why the map will not map out the unitialized
variable in this code with 'str'? Thanks a lot,
use strict;
use warnings;
sub total
{
my $firstone = shift;
my $sum = 0;
my $item = 0;
map { defined $_ ? $_ : 'str' } @_ ;
print @_;
}
my $hello;
print "not defined there\n" unless defined $hello;
my @data1 =('a','b','c','d',$hello);
total(@data1);
A call to map doesn't modify the object list - it simply returns the modified
list, and in this case you're simply throwing that result away. Take a look at
perldoc -q "grep in a void context"
as the same comments apply to map.
You need to do either
@_ = map { defined $_ ? $_ : 'str' } @_ ;
No need for separate scope there:
@_ = map defined() ? $_ : 'str', @_;
or
do {$_ = 'str' unless defined } for @x;
Or there:
defined() or $_ = 'str' for @x;
or you could just print the return value of map directly:
print map { defined $_ ? $_ : 'str' } @_ ;
Or there:
print map defined() ? $_ : 'str', @_;
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/