in reply to Error while taking 'Scalar of scalar'
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}); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Error while taking 'Scalar of scalar'
by bart (Canon) on Jul 05, 2007 at 12:36 UTC | |
|
Re^2: Error while taking 'Scalar of scalar'
by cool (Scribe) on Jul 04, 2007 at 16:25 UTC |