Perl is one language that it bittersweet to me. It is very powerful, has endless ability, millions of modules that add every possible feature you could want. My only complaint is that it is so flexible that it creates the perfect environment for bad code habits. Most of the worst code I have ever seen was done in Perl, and some of it was my code.
Coding in Perl is fun and it is a very powerful tool. If you have not done any Perl coding you should give it a try. For now here is my simple code to calculate standard deviation.
#!/usr/bin/perl use strict; #Prevent division by 0 error in case you get junk data exit undef unless(scalar(@ARGV)); # Step 1, find the mean of the numbers my $total1 = 0; foreach my $num (@ARGV) { $total1 += $num; } my $mean1 = $total1 / (scalar @ARGV); # Step 2, find the mean of the squares of the differences # between each number and the mean my $total2 = 0; foreach my $num (@ARGV) { $total2 += ($mean1-$num)**2; } my $mean2 = $total2 / (scalar @ARGV); # Step 3, standard deviation is the square root of the # above mean my $std_dev = sqrt($mean2); printf "%0.2f", $std_dev;
No comments:
Post a Comment