Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Error while taking 'Scalar of scalar'

by tirwhan (Abbot)
on Jul 04, 2007 at 12:07 UTC ( [id://624870]=note: print w/replies, xml ) Need Help??


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

All dogma is stupid.

Replies are listed 'Best First'.
Re^2: Error while taking 'Scalar of scalar'
by bart (Canon) on Jul 05, 2007 at 12:36 UTC
    You're trying to assemble scalar variable names at runtime. Don't do that.
    Obligatory link to the classic trio of articles by Dominus: varvarname
Re^2: Error while taking 'Scalar of scalar'
by cool (Scribe) on Jul 04, 2007 at 16:25 UTC
    Hash keeps surprising me. Excellent suggestion indeed :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://624870]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-18 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found