Re: data structure problem




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

.



Relevant Pages

  • Re: CDs play slow-distorted
    ... I have a sound and photo studio and when I get a "dull" ... If you find this response helpful, ... You tend to quote these two references willy nilly. ...
    (microsoft.public.windowsxp.music)
  • Re: Google Groups filtering emails?
    ... >>> references to stay within the 998 chars limit. ... >> Seems like they should leave the first one and strip from the second ... If you're going to QUOTE (and that's what those greter-than symbols ... The first message ID is very important as the starting point of ...
    (news.software.readers)
  • Re: Questions
    ... you don't like reading information you post here? ... get the quote aren't very familiar with the Prime or Primary source. ... together with scholars employed in respective town or area pre-view ... Others who looked up the other references for full also had a chance, ...
    (sci.archaeology)
  • Re: Scientists against Bush or Swift Boats against Kerry - whom would you believe? Tin cans dont hav
    ... >What more context could you need? ... >> You did not provide any quote at all for Kerry's first contact with the ... >Kerry that they are willing to ignore the contradictions in his OWN ...
    (sci.space.policy)
  • Re: OT 9/11 Report
    ... Once, twice, thrice, et boring cetera, you have ... been unable to give the references. ... Are you afraid because you can't ... find it or quote it? ...
    (alt.autos.toyota)