Solved

No clue about the error...

Posted on 2000-02-21
5
268 Views
Last Modified: 2010-03-05
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
Comment
Question by:clo1
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:RobWMartin
ID: 2544915
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
 
LVL 84

Expert Comment

by:ozo
ID: 2545001
# try:
$graph -> plot([[0..$#x],[@x]]);
0
 
LVL 2

Author Comment

by:clo1
ID: 2545015
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
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 2545066
#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
 
LVL 2

Author Comment

by:clo1
ID: 2545070
Thanks
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to display a matrix report for 3-dimenional array 3 73
Perl script to parse xml file 2 132
crawling gofundme 4 145
Perl script to process a .csv file 18 66
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question