data structure problem

Z

zhilianghu

A tutorial for using GD::Graph
(http://linuxgazette.net/issue83/padala.html) describes the input data
structure as:

my @data = (['Project', 'HW1', 'HW2', 'HW3', 'MidTerm', 'Final'],
[25, 6, 7, 2, 25, 35]);

In my program I constructed the data from internal associative array
as:

--1--
while (<HANDLE>) {
.... .......
$namess .= "'$key' ";
$values .= "$counts{$key} ";
}
$namess =~ s/\,$//g; # Take out the last comma
$values =~ s/\,$//g;

--2--
my @data = "([$namess],[$values])";

Although the print out of the "@data" structure appears the same as in
the example, the program complains: "Invalid data set: 0 at
/usr/local/apache/cgi-bin/treer line 153."

I also tried to use "push" at step "--1--", and/or use "qw" at step
"--2--", trying to make it an array but all failed the data demanded by
the next step ($myimage = $chart->plot(\@data)). I wonder what was the
problem? What the data structure in the example really ask for, and
the trick in producing such data structure in a dynamic way?

Thanks in advance for any kind advice!

Joe
 
P

Paul Lalli

A tutorial for using GD::Graph
(http://linuxgazette.net/issue83/padala.html) describes the input data
structure as:

my @data = (['Project', 'HW1', 'HW2', 'HW3', 'MidTerm', 'Final'],
[25, 6, 7, 2, 25, 35]);

In my program I constructed the data from internal associative array
as:

--1--
while (<HANDLE>) {
.... .......
$namess .= "'$key' ";
$values .= "$counts{$key} ";
}
$namess =~ s/\,$//g; # Take out the last comma
$values =~ s/\,$//g;

--2--
my @data = "([$namess],[$values])";

You have very much misunderstood the sample data. The array is not an
array of strings with parentheses and brackets. It is an array
containing two array references. Before you go any further at all, you
need to stop and learn about references:
perldoc perlreftut
perldoc perlref

I'm serious here. Don't go any further until you've read at least the
first one of those documents. You will not be able to do anything with
this program until you understand what references are, how to construct
them, and how to use them.

Once you've read and understood about references, try your program
again. If you continue to have problems, post *that* attempt - in a
short-but-complete script that we can run, and describe your error.

Paul Lalli
 
A

A. Sinan Unur

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
A tutorial for using GD::Graph
(http://linuxgazette.net/issue83/padala.html) describes the input data
structure as:

my @data = (['Project', 'HW1', 'HW2', 'HW3', 'MidTerm', 'Final'],
[25, 6, 7, 2, 25, 35]);

In my program I constructed the data from internal associative array
as:

--1--
while (<HANDLE>) {
.... .......
$namess .= "'$key' ";
$values .= "$counts{$key} ";
}
$namess =~ s/\,$//g; # Take out the last comma
$values =~ s/\,$//g;

--2--
my @data = "([$namess],[$values])";
....

I wonder what was the problem?

Why do you think that an array of references to anonymous arrays should
be equivalent to a single string?

Sinan
 
Z

zhilianghu

Indeed I have trouble to understand "references" although I read
through both perldoc pages, since I think I am not using it (I used
double quote to get values out of variables). However I learn better
with examples-trials-fails ... I try to reproduce what I did in a small
program as follows:

#!/usr/local/bin/perl -w

use GD::Graph::pie;

@rawdata =
('X','X','W','X','R','R','R','Y','Y','W','X','R','R','R','Y');

##
## Count the occurance of each letter into a hash
##
my(%count);
foreach $letter(@rawdata) {
$count{$letter}++;
}

## Example data structure----------------------------
## my @data = (['W', 'X', 'Y', 'H', 'M', 'F'],
## [25, 6, 7, 2, 25, 35]);
##-----------------------------------------------------

##
## Extract counts and names to formulat data
##
foreach my $key (sort {$count{$b} <=> $count{$a}} keys %count) {
$letter .= "$key,";
$counts .= "$count{$key},";
}
$letter =~ s/,$//g;
$counts =~ s/,$//g;
my @data = "([$letter], [$counts])";
## print "@data\n";

##
## Draw the pie
##
my $mygraph = GD::Graph::pie->new(300, 300);
my $myimage = $mygraph->plot(\@data) or die $mygraph->error;
open(PIE,">pie.png");
binmode PIE;
print PIE $myimage->png;
close(PIE);

It would be much appreciated if you could kindly pick out the lines
that follow wrong ideas and why. Any work-around will be appreciated
too.

Joe
 
A

A. Sinan Unur

Please read the posting guidelines for this group, and quote an
appropriate amount of context when you reply.

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
Indeed I have trouble to understand "references" although I read
through both perldoc pages, since I think I am not using it (I used
double quote to get values out of variables). However I learn better
with examples-trials-fails ...

Trying random stuff until it "works" is not learning.
I try to reproduce what I did in a small program as follows:

#!/usr/local/bin/perl -w

use strict;

missing
use GD::Graph::pie;

@rawdata =
('X','X','W','X','R','R','R','Y','Y','W','X','R','R','R','Y');

my @rawdata = qw( X X W X R R R Y Y W X R R R Y );
##
## Count the occurance of each letter into a hash
##
my(%count);
foreach $letter(@rawdata) {
$count{$letter}++;
}

my %count;
$count{$_}++ for @rawdata;


## Example data structure----------------------------
## my @data = (['W', 'X', 'Y', 'H', 'M', 'F'],
## [25, 6, 7, 2, 25, 35]);
##----------------------------------------------------- ....
foreach my $key (sort {$count{$b} <=> $count{$a}} keys %count) {
$letter .= "$key,";
$counts .= "$count{$key},";
}

Why do you think creating a string is the same as creating a list of
references to anonymous arrays?
$letter =~ s/,$//g;
$counts =~ s/,$//g;
my @data = "([$letter], [$counts])";
## print "@data\n";

use Data::Dumper;
print Dumper \@data;

will show you what you actually have.

my $myimage = $mygraph->plot(\@data) or die $mygraph->error;

my $myimage = $mygraph->plot([\@letters, \@counts]);

OK. All of that is untested code. I hope there aren't any glaring
errors.

Just a question ... Do you understand the difference between:

my $x = [ 4 ];

and

my $x = '[ 4 ]';

If you do not, then I have given you just a fish, and you don't have
much hope unless you sit down, read, and actually work your way through
simple exercises.

Sinan
 
J

J. Gleixner

Indeed I have trouble to understand "references" although I read
through both perldoc pages, since I think I am not using it

Do more than read them, try the code or make up some code so that you
understand arrays, hashes, and references to them.

This:
## Example data structure----------------------------
## my @data = (['W', 'X', 'Y', 'H', 'M', 'F'],
## [25, 6, 7, 2, 25, 35]);
##-----------------------------------------------------

and this:
my @data = "([$letter], [$counts])";

Are not even close to the same. Give perldsc a read to help figure out
data structures.

To get right to the point, you could build your arrays:

my ( @labels, @values );
push( @labels, 'W', 'X', 'Y' );
push( @values, 26, 6, 7 );

# then assign them to @data.
my @data = ( \@labels, \@values );


You'll find a lot of examples around the Internet ( there's a search
engine called Google, that might be useful. :) as well as in the
distribution you used to install GD::Graph. Those examples also are
conveniently found on CPAN:
http://search.cpan.org/src/BWARFIELD/GDGraph-1.4305/samples/
 
Z

zhilianghu

Thanks much to all who tried to help! It's much appreciated.

I think I know ( ;-) array, hash, but start to have difficulties with
"reference". I have 3 books: Learning Perl, Programming Perl, and Perl
CookBook for several years and found it is hard to chip in on
"reference" and "map", although I searched and found tons of excellent
tutorial stuff online but still didn't get into it. I thought I could
get around without it ... well, here I was trapped.

With J. Gleixner's hint I was able to get my program to work, and A.
Sinan's

use Data::Dumper;
print Dumper \@data;

did give me a feel of what I had (useful thing to me!). I think this is
just starting point for me to learn and read more intellegently!

Have a nice weekend my friends!

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,940
Messages
2,570,108
Members
46,574
Latest member
LayneChew

Latest Threads

Top