Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Mean, Median and Mode

by vinoth.ree (Monsignor)
on Jun 16, 2014 at 07:03 UTC ( [id://1089995]=note: print w/replies, xml ) Need Help??


in reply to Mean, Median and Mode

Hi,

Is this your questions?

How to open a faketext.txt file?

How to save the mean, median and mode data into hash?


All is well

Replies are listed 'Best First'.
Re^2: Mean, Median and Mode
by Noob@Perl (Novice) on Jun 16, 2014 at 07:07 UTC

    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.

      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"; }

      All is well

        With split, chomp job here would have been taken care off.

        More so, it would be wonderful, if the OP tell what the content of his file 'faketext.txt' is. Is he counting the numbers of lines in the file? Is he getting different value from each line? Maybe a peek into the file would have helped.

        If the OP file is CSV file, then Text::CSV_XS would be alot better than splitting on commas.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

      How can I open the file faketext.txt in the script,

      perlintro , open...

        I added open chomp and push, does it make sense? will the file be readable by the script now?

        #!/usr/bin/perl -w open ( FH, 'faketext.txt' ); while ( <FH> ) { chomp; push @fake, $_; use strict; 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 ); print "Please enter data, separated by commas: "; $data = <STDIN>; @dataset = split( /[\s,]+/, $data ); print "Median: ", median(@dataset), "\n"; print "Mean: ", mean(@dataset), "\n"; print "Standard Dev.: ", std_dev(@dataset), "\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1089995]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-24 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found