How to deal with $$genecolor issue
You're trying to assemble scalar variable names at runtime. Don't do that. Rather, change your sub to
my %color = ( lavender => $im->colorAllocate(230,230,255), lightblue => $im->colorAllocate(173,216,230), honeydew => $im->colorAllocate(240,255,240) ); while (@ary) { my $genecolor= coloring (); $im->filledRectangle($X1,$Y1-20,$X2,$Y2,$color{$genecolor});
Other things which could be improved in your code: use for instead of while when iterating over an array, use scalar @color instead of $#color in your subroutine, pass the array of possible colors into the subroutine instead of maintaining it in two places in your code. E.g.
sub coloring{ my @color= @{$_[0]}; my $color_no=int(rand(scalar @color)); return $color[$color_no]; }
my %color = ( lavender => $im->colorAllocate(230,230,255), lightblue => $im->colorAllocate(173,216,230), honeydew => $im->colorAllocate(240,255,240) ); for (@ary) { my $genecolor= coloring ([keys %color]); $im->filledRectangle($X1,$Y1-20,$X2,$Y2,$color{$genecolor}); }
In reply to Re: Error while taking 'Scalar of scalar'
by tirwhan
in thread Error while taking 'Scalar of scalar'
by cool
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |