Solved

No clue about the error...

Posted on 2000-02-21
5
262 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

770 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