Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

how can you put this?

by Anonymous Monk
on Jun 27, 2013 at 18:06 UTC ( #1041065=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
In the GD::Graph module, how can I put an array of colours in the  dclrs attribute?
I mean, instead of saying:
$graph->set( 'dclrs' => [ qw(lblue lyellow blue yellow lgreen lred green red purple orange pink dyellow) ]

if I have an array, like:
@colour_array = ("lblue", "lyellow", "blue", "yellow", "lgreen", "lred +", "green", "red", "purple", "orange", "pink", "dyellow");

can I pass it inside?

Comment on how can you put this?
Select or Download Code
Re: how can you put this?
by tobyink (Abbot) on Jun 27, 2013 at 18:21 UTC
    $graph->set(dclrs => \@colour_array)

    or:

    $graph->set(dclrs => [@colour_array])

    There's a slight difference between the two; the first gives $graph a reference to your original array, so $graph could theoretically modify it (push elements onto it, for example). The second creates an empty array, copies from your original array into the new one, and gives $graph a reference to the new array. That way your original array is safe from any modification.

    Which one you choose depends on whether you want to allow $graph to be able to modify @colour_array at all. If you don't care whether it does, then use the first as it will be very marginally faster.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: how can you put this?
by Eily (Deacon) on Jun 27, 2013 at 18:22 UTC

    Sure. What the first bit of code does is use an (anonymous) array ref. What you want to do is use a reference to your array. $graph->set( dclrs => \@colour_array );

    Upgrade : Oh well, just go along with tobyink's answer, since it's more complete.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-20 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls