How to initialize an array? I want to get a median by using 
Statistics::Descriptive.
I enter (12, 13, 14), (15, 16, 17), (16, 17, 18)
I expect 13,16,17. Instead,
I get 13.00, 14.50, 16.00.

I thought my @temp_array=(); in the median sub (see code snippet below) will 
get me new array. But I am wrong. How do I initialize an array in my example?

Thanks for helps.

-Eugene


####  code snippet #####

use Statistics::Descriptive;
my $stat = Statistics::Descriptive::Full->new();
use strict;
use warnings;

my $i;
sub median {
        my $count = $_[0];
        my @temp_array=();
        print "\nthe number of item in array: ", $stat->count();
        for ($i=0; $i<$count; $i++) {
                print "\nenter a number: "; my $temp_C=<STDIN>;
                push @temp_array, $temp_C
        }
        $stat->add_data(@temp_array); 
        my $median = $stat->median();
        my $num = $stat->count();
        print "\nmedian: $median of $num items";
        return $median;
}
my $T0 = sprintf "%.2f", median(3);
my $T1 = sprintf "%.2f", median(3);
my $T2 = sprintf "%.2f", median(3);
print "\n$T0\t$T1\t$T2";


#### output ##########
the number of item in array: 0
enter a number: 12
enter a number: 13
enter a number: 14
median: 13
 of 3 items
the number of item in array: 3
enter a number: 15
enter a number: 16
enter a number: 17
median: 14.5 of 6 items
the number of item in array: 6
enter a number: 16
enter a number: 17
enter a number: 18
median: 16
 of 9 items

13.00   14.50   16.00

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to