• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

No clue about the error...

I have written a perl program which is using the 50 values in the input file and generate a graph using the GD::Graphics::lines module in perl. But the complier always shows up the following error. Please help
----------------------------------------
Can't use string ("0.990411") as an ARRAY ref while "strict refs" in use at C:/Perl/site/lib/GD/Graph.pm line 278.
----------------------------------------
This is my code

#!c:\perl\bin\perl

use GD::Graph::lines;

my $graph = new GD::Graph::lines();
my $x_size = 50;
my @x;

open (READ, "<c:/cygnus/cygwin-b20/output.txt")
  or die "Cannot open file for input";
while (my $line = <READ>) {
  chomp $line;
  push @x, $line;
}
close READ;

$graph -> set(
      x_label => 'x',
      y_label => 'j0(x)',
      title => 'Bessel Function',
);

$graph -> plot(\@x);
save_graph($graph);

sub save_graph(){
  my $chart = shift;
  my $name = 'Bessel';
  my $ext = $chart -> export_format;
  open (GRAPH, ">c:/cygnus/cygwin-b20/$name.$ext");
  binmode GRAPH;
  print GRAPH $chart -> gd -> $ext();
  close GRAPH;
}
--------------------------------------
This is the content of my input file.

0.990411
0.961921
0.915348
0.852026
0.773763
0.682786
0.581663
0.473230
0.360497
0.246553
0.134472
0.027218
-0.072448
-0.162050
-0.239468
-0.303004
-0.351423
-0.383987
-0.400468
-0.401147
-0.386791
-0.358622
-0.318264
-0.267686
-0.209121
-0.144996
-0.077838
-0.010194
0.055460
0.116798
0.171722
0.218425
0.255448
0.281721
0.296591
0.299836
0.291657
0.272668
0.243857
0.206548
0.162342
0.113055
0.060645
0.007143
-0.045428
-0.095121
-0.140139
-0.178895
-0.210066
-0.232638
---------------------------------------
Please help me to solve the error message. Thanks !!
0
clo1
Asked:
clo1
  • 2
  • 2
1 Solution
 
RobWMartinCommented:
Apparently the $graph->plot() subrouting is expecting an array of arrays, like so:

[[foo1,bar1][foo2,bar2]...]

You are giving it something like:

[foo,bar,baz,...]

where foo is 0.990411.  The Graph module is trying to dereference 0.990411 as a reference to an array, like [foo1,bar1] above.  It can't do that and is complaining.  

I don't know anything about GD::Graphics::lines, so I don't know what structure the plot method is looking for in it's argument.  So, that's as far as I can take it.  Sorry.

0
 
ozoCommented:
# try:
$graph -> plot([[0..$#x],[@x]]);
0
 
clo1Author Commented:
Thanks ozo, it works. But can you tell me what going on in this line of statement before I accept your comment as answer and give you an A? Appreciate for all the help.
0
 
ozoCommented:
#actually, it looks like
 $graph -> plot([[map{$_%5?'':$_/5}1..@x],[@x]]);
#might be better labels for your input values.

As explained by RobWMartin and by
perldoc GD::Graph
plot s expecting an array of arrays

          USAGE
                 Fill an array of arrays with the x values and  the  values
                 of  the data sets.  Make sure that every array is the same
                 size, otherwise GD::Graph  will  complain  and  refuse  to
                 compile the graph.

                   @data = (
                     ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
                     [    1,    2,    5,    6,    3,  1.5,    1,     3,     4]
                     [ sort { $a <=> $b } (1, 2, 5, 6, 3, 1.5, 1, 3, 4) ]
                   );

                 If  you  don't  have  a  value  for  a  point in a certain
                 dataset, you can use undef, and the point will be skipped.

0
 
clo1Author Commented:
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now