Re: data structure problem
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 27 Jan 2006 23:09:20 GMT
Please read the posting guidelines for this group, and quote an
appropriate amount of context when you reply.
zhilianghu@xxxxxxxxx wrote in news:1138401095.052674.150780
@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 @letters = sort { $count{$b} <=> $count{$a} } keys %count;
my @counts = @count{ @letters };
> 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
--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
.
- References:
- data structure problem
- From: zhilianghu
- Re: data structure problem
- From: Paul Lalli
- Re: data structure problem
- From: zhilianghu
- data structure problem
- Prev by Date: FAQ 3.6 How do I profile my Perl programs?
- Next by Date: Re: Problems passing a reference to a hash between functions
- Previous by thread: Re: data structure problem
- Next by thread: Re: data structure problem
- Index(es):
Relevant Pages
|
|