histogram and perl

From: Andrea Spitaleri (spiritelllo_at_interfree.it)
Date: 07/30/04


Date: 30 Jul 2004 03:23:43 -0700

Hi folks,
I am stuck with a problem. Here it is:
I have got an @array1 and I would like to calculate the frequency of
its element by a range. In few words, build an histogram.
this is my ex code:

#!/usr/bin/perl -w

@value = (48,49,50,46,47,47,35,38,40,42,45,47,48,44,43,46,45,42,43,47);

@sort = sort {$a<=>$b} @value;

$last=$sort[$#sort];

$range = $last - $sort[0];

$classes=5;

$interval = $range / $classes;

$count=0;
$tot=$sort[0];
while ($count<$classes){
    # print "$tot\n";
    $i=$tot;
    $tot=$tot+$interval;
    $hash{$i}=$tot;
    # print "$tot\n";
    # print "---\n";
    ++$count;
}

$j=1;
@new=$sort[0];
foreach $key (sort{$hash{$a}<=>$hash{$b}} keys %hash){
    print "interval $j $key =>> $hash{$key}\n";
    push @new,$hash{$key};
    ++$j;
}

# that's wrong..I know :) but it is just to explain what I am trying
to do
#in fact it gives weird results, as 35 > 40...

for ($im=0;$im<$#sort;++$im){
    foreach $key (sort{$hash{$a}<=>$hash{$b}} keys %hash){
        if (($sort[$im]>$key) && ($sort[$im]<$hash{$key})){
            print "$sort[$im]>$key && $sort[$im]<$hash{$key}\n";
        }
    }
}

./hystogram.pl

interval 1 35 =>> 38
interval 2 38 =>> 41
interval 3 41 =>> 44
interval 4 44 =>> 47
interval 5 47 =>> 50

what I don't know how to do it is to calculate the frequency of my
@value element respect to the five intervals. I should get an output
like that, with an extra column, which represents the frequency:

interval 1 35 =>> 38 1
interval 2 38 =>> 41 2
interval 3 41 =>> 44 4
interval 4 44 =>> 47 5
interval 5 47 =>> 50 8

thanks for any help

regards

andrea

ps. BTW, I need to feed then gnuplot for making the graph.



Relevant Pages

  • Re: /clr option
    ... I have 2 lines of .NET code in the file with some c code. ... i tried the first option and got stuck on some ... yacc/lex geneate a union and the compiler complained about its layout being ...
    (microsoft.public.dotnet.languages.vc)
  • Re: message stuck in Categorizer
    ... Is there anything to unstick the messages that are stuck in the ... > is not found in the Microsoft Exchange Server 2003 organization default ... > Best Regards, ...
    (microsoft.public.windows.server.sbs)
  • Re: No Pseudo-Pilots
    ... Regards ... Ken S. Tucker ... And the fact that you're stuck to a chair is proof that super glue ...
    (rec.aviation.student)
  • Re: How to evaluate in C# a string of expression
    ... It's a freeware product containing method EvaluateExpression. ... Regards, ... I've been stuck on this issue for months. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Removing contact items
    ... I have written the below code but am stuck at how to check for the condition ... and how to delete individual contact that fits the condition. ... Regards ... Dim O As Outlook.Application ...
    (microsoft.public.outlook)