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);


Some options:

print map { defined $_ ? $_ : 'str' } @_ ;
@_ = map { defined $_ ? $_ : 'str' } @_ ;
map { $_ = 'str' unless defined } @_ ;

But people will tell you that using map in void context
(option 3) is wrong.

Your mistake was thinking that map sets $_ to the return
value of the block.  Instead, you must set $_ inside the
block if that's what you want.

--
Brad

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


Reply via email to