Perl percent calculation

If you have an array in Perl with values and want to check what the percentages of them you could use this little function:

#!/usr/bin/perl
use strict;
#example data
my @arr = (3,5,7,8,0,20,2,1,-1);
#test sub
print percentage(@arr);
# calculate percentage
sub percentage {
    my @arr = @_;
    my $out = "";
    @arr = sort { $b <=> $a } @arr;
    my $max_value=$arr[0];
    for my $i (0..$#arr){
        $out .= $arr[$i];
        $out .= " = ".sprintf("%.2f",(($arr[$i]/$max_value)*100))."%".$/;
    }
    return($out);
}

result looks like this:

20 = 100.00%
8 = 40.00%
7 = 35.00%
5 = 25.00%
3 = 15.00%
2 = 10.00%
1 = 5.00%
0 = 0.00%
-1 = -5.00%

 

To make a table to compare all together:

#!/usr/bin/perl
use strict;
#example data
my @arr = (3,5,7,8,0,20,2,1,-1);
#test sub
print percentage(@arr);
# calculate percentage
sub percentage {
    my @arr = @_;
    my $space = 10;
    my $out = len_format("Percent",$space);
    @arr = sort { $b <=> $a } @arr;
    for my $x (0..$#arr){$out .= len_format($arr[$x],$space);}
    $out .= $/;
    for my $i (0..$#arr){
        $out .= len_format($arr[$i],$space);
        for my $x (0..$#arr){
            if($arr[$x] != 0 && $arr[$i] != 0){
                $out .= len_format(sprintf("%.2f",(($arr[$x]/$arr[$i])*100))."%",$space);
            }else{
                $out .= len_format("?%",$space);
            }
        }
        $out .= $/;
    }
    return($out);
}
#format strings
sub len_format{
    my $str = shift;
    my $len = shift;
    return((" "x($len-length($str))).$str);
}

For the format subs look at Perl format string to the same size.

result looks like this:

   Percent        20         8         7         5         3         2         1         0        -1
        20   100.00%    40.00%    35.00%    25.00%    15.00%    10.00%     5.00%        ?%    -5.00%
         8   250.00%   100.00%    87.50%    62.50%    37.50%    25.00%    12.50%        ?%   -12.50%
         7   285.71%   114.29%   100.00%    71.43%    42.86%    28.57%    14.29%        ?%   -14.29%
         5   400.00%   160.00%   140.00%   100.00%    60.00%    40.00%    20.00%        ?%   -20.00%
         3   666.67%   266.67%   233.33%   166.67%   100.00%    66.67%    33.33%        ?%   -33.33%
         2  1000.00%   400.00%   350.00%   250.00%   150.00%   100.00%    50.00%        ?%   -50.00%
         1  2000.00%   800.00%   700.00%   500.00%   300.00%   200.00%   100.00%        ?%  -100.00%
         0        ?%        ?%        ?%        ?%        ?%        ?%        ?%        ?%        ?%
        -1 -2000.00%  -800.00%  -700.00%  -500.00%  -300.00%  -200.00%  -100.00%        ?%   100.00%

 

If you want some colours see this:

use strict;
#example data
my @arr = (3,5,7,8,0,20,2,1,-1);
#test sub
print percentage(@arr);
# calculate percentage
sub percentage {
    my @arr = @_;
    my $space = 10;
    my $out = len_format("Percent",$space);
    @arr = sort { $b <=> $a } @arr;
    for my $x (0..$#arr){$out .= "e[33m".len_format($arr[$x],$space)."e[37m";}
    $out .= $/;
    for my $i (0..$#arr){
        $out .= "e[33m".len_format($arr[$i],$space)."e[37m";
        for my $x (0..$#arr){
            $out .= "e[36m" if($i%2 == 0);
            if($arr[$x] != 0 && $arr[$i] != 0){
                $out .= len_format(sprintf("%.2f",(($arr[$x]/$arr[$i])*100))."%",$space);
            }else{
                $out .= len_format("?%",$space);
            }
            $out .= "e[37m";
        }
        $out .= $/;
    }
    return($out);
}
#format strings
sub len_format{
    my $str = shift;
    my $len = shift;
    return((" "x($len-length($str))).$str);
}

And now we have the result with colours:

Screenshot_percentageFor more infos about colours see at Perl Terminal Colours.

 

And now we use Perl Formater for Terminal and HTML to make this table:

Percent 20 8 7 5 3 2 1 0 -1
20 100.00% 40.00% 35.00% 25.00% 15.00% 10.00% 5.00% ?% -5.00%
8 250.00% 100.00% 87.50% 62.50% 37.50% 25.00% 12.50% ?% -12.50%
7 285.71% 114.29% 100.00% 71.43% 42.86% 28.57% 14.29% ?% -14.29%
5 400.00% 160.00% 140.00% 100.00% 60.00% 40.00% 20.00% ?% -20.00%
3 666.67% 266.67% 233.33% 166.67% 100.00% 66.67% 33.33% ?% -33.33%
2 1000.00% 400.00% 350.00% 250.00% 150.00% 100.00% 50.00% ?% -50.00%
1 2000.00% 800.00% 700.00% 500.00% 300.00% 200.00% 100.00% ?% -100.00%
0 ?% ?% ?% ?% ?% ?% ?% ?% ?%
-1 -2000.00% -800.00% -700.00% -500.00% -300.00% -200.00% -100.00% ?% 100.00%

One thought on “Perl percent calculation”

  1. Hi, I want to subscribe for this web site to get most recent updates, so where can i do it please help out.

Leave a Reply

Your email address will not be published.

one × two =