Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Error while taking 'Scalar of scalar'

by cool (Scribe)
on Jul 04, 2007 at 11:59 UTC ( #624869=perlquestion: print w/replies, xml ) Need Help??

cool has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
For each iteration of my foreach loop, I want a different color...
but I am getting error

Can't use string ("lightblue") as a SCALAR ref while "strict refs" in use at a.pl line 184, <STDIN> line 2.

I am using a subroutine
sub coloring{ my @color= qw( lavender lightblue honeydew); my $color_no=int(rand($#color)); return $color[$color_no]; }
In main code, calling my sub
my $lavender=$im->colorAllocate(230,230,255); my $lightblue=$im->colorAllocate(173,216,230); my $honeydew=$im->colorAllocate(240,255,240); while(@ary) { my $genecolor= coloring (); $im->filledRectangle($X1,$Y1-20,$X2,$Y2,$$genecolor); }
How to deal with $$genecolor issue?? Pl help/suggest.

Replies are listed 'Best First'.
Re: Error while taking 'Scalar of scalar'
by tirwhan (Abbot) on Jul 04, 2007 at 12:07 UTC
    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.
      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
      Hash keeps surprising me. Excellent suggestion indeed :)
Re: Error while taking 'Scalar of scalar'
by tinita (Parson) on Jul 04, 2007 at 12:15 UTC
    my @color= qw( lavender lightblue honeydew); my $color_no=int(rand($#color)); return $color[$color_no];
    have you ever wondered why you only get back the first two colors? rand(x) returns a random number between 0 and (excluding) x.

    the usual idiom for this in perl is:

    my @colors = qw(lavender lightblue honeydew); my $color = $colors[rand @colors]; return $color;
      my $color = $colors[rand @colors];
      Or
      my $color = $colors[int rand @colors];
      which does the exact same thing, but doesn't depend on the knowledge that numbers used as array indexes are truncated to integer, instead of rounded.
Re: Error while taking 'Scalar of scalar'
by naikonta (Curate) on Jul 04, 2007 at 12:14 UTC
    The coloring() subroutine returns a string (simple scalar) contains the color name, which you store in $genecolor scalar variable. You pass in the filledRectangle() method the $$genecolor, which means you're trying to dereference $genecolor. But $genecolor is a simple scalar, not a reference, hence the error.

    If you look up the perldiag, or in your local system, man perldiag or perldoc perldiag, you'll find the error explanation:

    Can't use string ("%s") as %s ref while "strict refs" in use (F) Only hard references are allowed by "strict refs". Symbolic ref +erences are disallowed. See perlref.
    If you put use diagnostics; line near the top of your program, it will give you the error explanation automatically.

    I don't know what the filledRectangle() method expects as the fifth argument. If it says a SCALAR reference or a reference to a SCALAR then you must pass it as:

    $im->filledRectangle($X1,$Y1-20,$X2,$Y2,\$genecolor);
    Otherwise, if it says nothing about references, then just pass in the variable as is:
    $im->filledRectangle($X1,$Y1-20,$X2,$Y2,$genecolor);

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Error while taking 'Scalar of scalar'
by jettero (Monsignor) on Jul 04, 2007 at 12:16 UTC
    Quasi-relevant: my $randcolor = $colors[ int rand $#colors ]; misses the last one every time. :(

    You might want my $randcolor = $colors[ rand @colors ]; instead.

    -Paul

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624869]
Approved by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-02-19 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (81 votes). Check out past polls.

    Notices?