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' } @_ ;

or

  do {$_ = 'str' unless defined } for @x;

or you could just print the return value of map directly:

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

HTH,

Rob

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


Reply via email to