A
Andrea Spitaleri
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.
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.