Re: Mean, Median and Mode
by wjw (Priest) on Jun 16, 2014 at 07:27 UTC
|
#!/usr/bin/perl -w
use strict;
use warnings;
sub mean {
my (@data) = @_;
my $sum;
foreach (@data) {
$sum += $_;
}
return ( $sum / @data );
}
sub median {
my (@data) = sort { $a <=> $b } @_;
if ( scalar(@data) % 2 ) {
return ( $data[ @data / 2 ] );
} else {
my ( $upper, $lower );
$lower = $data[ @data / 2 ];
$upper = $data[ @data / 2 - 1 ];
return ( mean( $lower, $upper ) );
}
}
sub std_dev {
my (@data) = @_;
my ( $sq_dev_sum, $avg ) = ( 0, 0 );
$avg = mean(@data);
foreach my $elem (@data) {
$sq_dev_sum += ( $avg - $elem )**2;
}
return ( sqrt( $sq_dev_sum / ( @data - 1 ) ) );
}
my ( $data, @dataset,%h,$lc );
my $file = "fake.txt";
open(IN, "<", $file) or die "can't open $file $!\n";
while (<IN>) {
chomp;
$lc++;
$h{$lc} = $_;
}
close IN;
#while (<DATA>) {
# chomp;
# $lc++;
# $h{$lc} = $_;
#}
#@dataset = (values %h);
print "vals " .scalar(keys %h) . "\n";
print "Median: ", median(@dataset), "\n";
print "Mean: ", mean(@dataset), "\n";
print "Standard Dev.: ", std_dev(@dataset), "\n";
__DATA__
1
4
2
7
89
5
2
8
6
23
If this assignment actually requires you to process the data in the subs by handing in the hash instead of the array, you should find enough here to get you started... . Run it with 'perl -d' to see what things look like. The debugger is easy to use.
One also has to presume that 'fake.txt' looks like __DATA__, which you did not specify.
...the majority is always wrong, and always the last to know about it...
Insanity: Doing the same thing over and over again and expecting different results...
A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is a facct
| [reply] [d/l] |
Re: Mean, Median and Mode
by Anonymous Monk on Jun 16, 2014 at 06:50 UTC
|
My question is how can I apply a hash to open a file name faketext.txt, read the lines and count them using mean, the mean, median and mode?
By doing it in a way that makes sense to you?
What are you going to use the hash for?
| [reply] |
|
|
| [reply] [d/l] |
Re: Mean, Median and Mode
by vinoth.ree (Monsignor) on Jun 16, 2014 at 07:03 UTC
|
Hi,
Is this your questions?
How to open a faketext.txt file?
How to save the mean, median and mode data into hash?
| [reply] |
|
|
Sorry after I posted the questions didn't really make sense to me either...Here they are, How can I open the file faketext.txt in the script, and making a hash to read the lines in the file so they can be counted by mean, median and mode.
| [reply] |
|
|
There are two forms of the open() function in Perl 5. The modern version takes three arguments, the filehandle to open or vivify, the mode of the filehandle, and the name of the file.
my $filename = "faketext.txt";
open my $fh, '<', $filename or die "Can't write to '$filename': $!\n";
After that use while loop and read the file content line by line and do the slite and call your mean,madian,etc functions like below,
while(my $eachLine = <$fh>)
{
chomp($eachLine);
my @dataset = split( /[\s,]+/, $eachLine );
print "Median: ", median(@dataset), "\n";
print "Mean: ", mean(@dataset), "\n";
print "Standard Dev.: ", std_dev(@dataset), "\n";
}
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
| [reply] |
|
|
|
|
Re: Mean, Median and Mode
by hexcoder (Curate) on Jun 16, 2014 at 22:07 UTC
|
Hello,
i used a bit of Frankenstein programming (a leg from vinoth.ree, an arm from the OP) to do what you probably want.
Each line from the file is split and then the length of the words are collected in @dataset.
Afterwards the whole array of word lengths is used to build
mean, median and std dev. So far no hash is needed, but you could of course use one to store the statistic properties in there.
The (untested) code is then like this:
#!/usr/bin/perl -w
use strict;
use warnings;
sub mean {
my (@data) = @_;
my $sum;
foreach (@data) {
$sum += $_;
}
return ( $sum / @data );
}
sub median {
my (@data) = sort { $a <=> $b } @_;
if ( scalar(@data) % 2 ) {
return ( $data[ @data / 2 ] );
} else {
my ( $upper, $lower );
$lower = $data[ @data / 2 ];
$upper = $data[ @data / 2 - 1 ];
return ( mean( $lower, $upper ) );
}
}
sub std_dev {
my (@data) = @_;
my ( $sq_dev_sum, $avg ) = ( 0, 0 );
$avg = mean(@data);
foreach my $elem (@data) {
$sq_dev_sum += ( $avg - $elem )**2;
}
return ( sqrt( $sq_dev_sum / ( @data - 1 ) ) );
}
my $filename = "faketext.txt";
open my $fh, '<', $filename or die "Can't read from '$filename': $!\n"
+;
my @dataset;
while(my $eachLine = <$fh>)
{
chomp($eachLine);
push @dataset, map { length($_) } split( /[\s,]+/, $eachLine );
}
print "Median: ", median(@dataset), "\n";
print "Mean: ", mean(@dataset), "\n";
print "Standard Dev.: ", std_dev(@dataset), "\n";
| [reply] [d/l] [select] |