Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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:

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?

Replies are listed 'Best First'.
Re: how can you put this?
by tobyink (Abbot) on Jun 27, 2013 at 18:21 UTC
    $graph->set(dclrs => \@colour_array)


    $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 (Parson) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041065]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2017-08-17 19:46 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (292 votes). Check out past polls.