Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to pass arrays into this program?

by Anonymous Monk
on Dec 12, 2013 at 13:04 UTC ( #1066836=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Monks!
I have made this script for creating histogram plots:
sub save_chart { my $chart = shift or die "Need a chart!"; my $name = shift or die "Need a name!"; local(*OUT); my $ext = 'png'; open(OUT, ">$name.$ext") or die "Cannot open $name.$ext for write: $!"; binmode OUT; print OUT $chart->gd->$ext(); close OUT; } use GD::Graph::bars; use GD::Graph::hbars; use GD::Graph::colour; my @data = ( ["name1", "name2", "name3","name4"], [23, 26, 53, 27], [77, 74, 47, 73] ); my @names = qw/sample15 sample15-h/; my $font_dir = '/usr/share/fonts/truetype'; my $font_file = "$font_dir/freefont/FreeSans.ttf"; for my $my_graph (GD::Graph::bars->new, GD::Graph::hbars->new(700,500) +) { my $name = shift @names; print STDERR "Creating image\n"; $my_graph->set( bgclr => 'white', box_axis => 0, y_max_value => 100, dclrs => ['#ff782a', '#6daa3a'], title => 'MY_TITLE', bar_width => 10, bar_spacing => 4, y_tick_number => 2, cumulate => 2, borderclrs => $my_graph->{dclrs}, bar_spacing => 4, transparent => 0, x_label_position => 0.5, tick_length => -4, axis_space => 6, x_ticks => 0, bar_spacing => 1, bargroup_spacing => 20, fgclr => '#bbbbbb', axislabelclr => '#333333', labelclr => '#333333', textclr => '#333333', legendclr => '#333333', legend_placement => 'DC', legend_marker_width => 12, legend_marker_height => 12, legend_spacing => 6, long_ticks => 0, borderclrs => [undef] ); $my_graph->set_title_font($font_file, 16); $my_graph->set_x_label_font($font_file, 16); $my_graph->set_y_label_font($font_file, 16); $my_graph->set_x_axis_font($font_file, 11); $my_graph->set_y_axis_font($font_file, 11); $my_graph->set_legend_font($font_file, 11); $my_graph->set_legend(qw(%leg1 %leg2)); $my_graph->plot(\@data); save_chart($my_graph, $name); }

Can you please tell me how I can pass the arrays in @data instead of hard-coding them inside the script? Because I want to make the script "universal"... I mean, if I have 3 arrays, namely @array_names, @array_numbers1 and @array_numbers2, how do I pass their values into the array @data?
Thank you!

Comment on How to pass arrays into this program?
Select or Download Code
Re: How to pass arrays into this program?
by BrowserUk (Pope) on Dec 12, 2013 at 13:16 UTC
    I have 3 arrays, namely @array_names, @array_numbers1 and @array_numbers2, how do I pass their values into the array @data?

    Where do you have these 3 arrays? Ie. In another script? In shell variables?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yeah, sorry!
      I meant, I want to read a tab-separated file and store the data that are now hard-coded into arrays... So, open the tab-separated file, create the 3 arrays and then pass them into @data, instead of writing their values by hand, like I do now...

        ... my @array_names = readfile( "somename" ); my @array_numbers1 = readfile( "someothername" ); my @array_numbers2 = readfile( "anothername" ); my @data = ( \@array_names, \@array_numbers1, \@array_numbers2 ); ...

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        It sounds like Text::CSV will do what you want.

        Please note that the majority of the code you posted has nothing to do with your question: this just leads to confusion (and perhaps frustration) when attempting to isolate the handful of relevant lines from the two screenfuls of code you posted. A better question will get better answers: the guidelines in "How do I post a question effectively?" can help you achieve this.

        -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-12-27 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (177 votes), past polls