4. Practical example

Below an example using two statistical modules.



#!/usr/bin/perl

use Statistics::Lite qw(:all);
use Algorithm::FastPermute ('permute');

sub validate {
	
    $data = shift;
    $data =~ s/\h+/ /g; # replace multiple horizontal space to one single space
    if($data =~ (/^[0-9 ]+$/) and (length($data) > 2) ) { # minimal two integers and a space, i.e. 1 2
      return 1;
    }
    else {
      return 0;
    }
}

$sep = "\n" . ("-" x 65) . "\n";

print($sep);
print("\UStatistics program");
print($sep);

print("Statistical functions:\n0 = count\n1 = frequencies\n2 = range\n3 = sum\n4 = mode\n5 = mean\n6 = median\n7 = variance\n8 = standard deviation\n9 = statsinfo\n10 = permutation");

print($sep);
print("\nEnter more than 2 positive integers, separated by a single space: ");
chomp($numbers = <STDIN>);

if(validate($numbers)) {
	
 print("\nEnter the number of your statistical function: ");
 chomp($stat = <STDIN>);

 print($sep);

 if($stat < 0 or $stat > 10) {
  print("Valid number statistical function required!\nSelect a number between 0 and 10 (inclusive).");
 }
 else
 {
   @data = split(" ", $numbers);
   
   # start 10 statistical functions 
   print("Count: " . count(@data)) if($stat == 0);  

   if($stat == 1) {
     %datas = frequencies(@data);
     print("No" . " " . "Freq" . "\n");
     foreach $k (keys(%datas)) {
       print($k . "  " . $datas{$k} . "\n");
     }
   }
 
   print("Range: " . range(@data)) if($stat == 2); 
   print("Sum: " . sum(@data)) if($stat == 3);  
   print("Mode: " . mode(@data)) if($stat == 4); 
   print("Mean: " . mean(@data)) if($stat == 5);
   print("Median: " . median(@data)) if($stat == 6);	
   print("Variance: " . variance(@data)) if($stat == 7);
   print("Standard deviation: " . stddev(@data)) if($stat == 8);  
   print("Statsinfo:\n" . statsinfo(@data)) if($stat == 9); 
 
   if($stat == 10) {

     sub factorial
     {
       $n = shift;
       $i = 1;
       $result = 1; 
       $result *= ++$i while $i < $n;      
       $result;
     }
  
     $no_elements = @data;
     $f = factorial($no_elements);

     permute {

       (++$count < $f) ? print "@data\n" : print "@data";	 

     } @data;
  
  } 
 
 }

} 
else {

 print("Valid data required!");

}

print($sep . "\n");