Solved

No clue about the error...

Posted on 2000-02-21
5
256 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
Comment Utility
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
Comment Utility
# try:
$graph -> plot([[0..$#x],[@x]]);
0
 
LVL 2

Author Comment

by:clo1
Comment Utility
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
Comment Utility
#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
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now