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

hi,

I tried to use URI-GoogleChart-1.02 in my perl program to draw picture. But I met a problem even after I tried for a long time.

my $chart = URI::GoogleChart->new("lines", 300, 100, data => [45, 80, 55, 68], range_show => "left", range_round => 1, );

It seems:

 data => [45, 80, 55, 68] # an anonymous array ref with comma

Because my data is an array or array_ref without comma, like: @a = (1 2 3 4), it will return error due to no comma.

I tried to add comma by join function: $y = join ',' , @a;. But it still doesn't work if I coded as

 data => [$y].

Do you have any suggestion how I can use the URI::GoogleChart module if I have an array without comma.

Many thanks, HH CHANG

Replies are listed 'Best First'.
Re: draw picture by google Chart
by kcott (Archbishop) on Oct 27, 2013 at 03:34 UTC

    G'day HH CHANG,

    You need to show the actual code you're running and the output it produces including any error and warning messages: see the "How do I post a question effectively?" guidelines for details.

    The code "@a = (1 2 3 4)" is syntactically incorrect and perl will abort compilation when it is encountered. The code will not run and "$y = join ',' , @a;" will be ignored:

    $ perl -e '@a = (1 2 3 4); $y = join ",", @a' Number found where operator expected at -e line 1, near "1 2" (Missing operator before 2?) Number found where operator expected at -e line 1, near "2 3" (Missing operator before 3?) Number found where operator expected at -e line 1, near "3 4" (Missing operator before 4?) syntax error at -e line 1, near "1 2" Execution of -e aborted due to compilation errors.

    If you're actually writing code like that, you may well have all sorts of other problems. From your last post, you seem to know about strict and warnings: use these pragmata in all your scripts.

    While I can't tell for certain from what you've posted here, a simplistic fix to the problem you describe may be:

    @a = (qw{1 2 3 4})

    -- Ken

Re: draw picture by google Chart
by AnomalousMonk (Archbishop) on Oct 27, 2013 at 03:55 UTC

    There's a lot about your post I don't understand, and I have no experience whatever with the URI::GoogleChart module, but I have never thought that mere ignorance should prevent one from offering advice, so here goes.

    First off, I assume the

    my $chart = URI::GoogleChart->new("lines", 300, 100, data => [45, 80, 55, 68], range_show => "left", range_round => 1, );
    statement works as it stands (possibly an example from documentation), and your problem is to use different data than 45, 80, 55, 68.

    ... my data is an array or array_ref without comma, like: @a = (1 2 3 4) ...

    As you have seen, this doesn't work because it is syntactically incorrect. The correct assignment expression would be  @a = (1, 2, 3, 4) or perhaps a statement like
        my @a = (1, 2, 3, 4);
    if one were defining a lexical array (see my) and initializing it. The quick fix is to put in the needed commas.

    But I suspect the quick fix doesn't work because your data is not in the form of a list, but rather is a string consisting of numbers with whitespace separating the numbers, something like  "1 2 3 4" (with the  " double-quotes being an essential part of the expression), e.g.,
        my $data = "1 2 3 4";

    If so, the question becomes "How do I convert a string in the format '1 2 3 4' into a list that I can assign to an array?" The split Perl built-in function can do just this with a string in this format. (Use something like Data::Dumper or Data::Dump to confirm your intermediate results.)

    >perl -wMstrict -le "use Data::Dumper; ;; my $data = '1 2 3 4'; my @a = split /\s+/, $data; print Dumper \@a; " $VAR1 = [ '1', '2', '3', '4' ];

    Now you have an array from which you can take a reference:  my $array_ref = \@a;
    This reference could be used in the  new() constructor of URI::GoogleChart:  data => $array_ref,
    or else the result of the referencing operation could be used directly:  data => \@a,
    or else the array itself could be expanded within an anonymous array constructor:  data => [ @a ],

    I hope that helps. Update: Oh, and please pay attention to kcott's good advice about effective question-posting and the use of strict and warnings!

      it's my fault. I should describe it more clearly.

      I know where is the problem.

      I parse a text and want to get identical words and its times. So I have a hash: key is "identical words" and value is "times".

      I hope I could draw a lines picture (x-axis is identical word and y-axis is times). In other words, "times" is data argument and "identical words" is chxl argument in URI::GoogleChart.

      my ($x, @y); $x="0:"; foreach (sort { $word2_hash{$b} <=> $word2_hash{$a} or $a cmp $b} +keys %word2_hash) { $x .= "|$_"; #add identical words push @y, $word2_hash{$_}; #add times } my $chart = URI::GoogleChart->new("lines", 400, 150, data => \@y,#it is OK to parse array_ref range_show => "left", range_round => 1, color => ["red"], chxl => "$x", # chxt => "x", ); #getstore($chart, "chart.png"); #print "get pic\n";

      <Sorry for my wrong post. It could work if use array_ref/p>

      I find where is my problem. The chxl seems have limit. If I decrease the identical words. I can get the picture. If I use a lot of identical words, I couldn't get the picture.

      Thanks for all your reply.