[oak perl] Can't modify map iterator in scalar assignment?

Kester Allen kester at gmail.com
Thu Dec 9 10:50:46 CST 2004


Hi Eric--

Are you trying to make $p an array reference?  If so, you should use
$p = [ qw ( 1 3 4 ) ];
or
$p = { 1, 3, 4 };
instead of what you're using.  Your statement assigns the value "4" to
$p.  If you "use warnings" (always a good idea) you'll get an error
"Useless use of a constant in void context at <filename> line
<linenum>"

Also, you're using map in a void context, which is generally frowned
upon.  A better phrasing might be:

if ( $p ) {
    $array[$_] = 'selected' foreach @{$p};
}
else {
    $array[0]='selected';
}

I can't tell from your question what you're using this for, but a hash
might be a better data structure for this:

my $p = [ qw(1 3 4) ];
my %hash = map { $_ => 'selected' } @$p;

--Kester


On Wed, 8 Dec 2004 17:43:02 -0800 (PST), Eric Chen <a1234_ec at yahoo.com> wrote:
> Hi,
> I have the following code
> 
> my $p=qw(1 3 4);
> my @array;
> if($p){
>   map{$array[$_]='selected'} @{$p};
> }
> else{
>   $array[0]='selected';
> }
> which works fine when I try to use triary operator
> 
>  ($p)? map{$array[$_]='selected'} @{$p} :
> $array[0]='selected';
> 
> I got Can't modify map iterator in scalbbar assignment
> 
> any ideas?
> 
> Thanks,
> Eric
> 
> __________________________________
> Do you Yahoo!?
> Meet the all-new My Yahoo! - Try it today!
> http://my.yahoo.com
> 
> _______________________________________________
> Oakland mailing list
> Oakland at mail.pm.org
> http://mail.pm.org/mailman/listinfo/oakland
>


More information about the Oakland mailing list