The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

# NAME

SimpleR::Stat

Simple Stat on arrayref, like sum, mean, calc rate, etc

# FUNCTION

## calc_rate_arrayref

``````    my @data = (3, 4, 1);
my \$r = calc_rate_arrayref(\@data);
dump(\$r);
#\$r:[0.375, 0.5, 0.125]``````

## transpose_arrayref(\$arr)

``````    my \$s= [ 1, 2 , 3 ], [ 4, 5 , 6 ] ];
my \$d = transpose_arrayref(\$s);
# \$d : [[1 ,4], [2, 5], [3 ,6 ]] ;``````

## sort_by_other_arrayref(\$arr, \$other_arr, \$map_sub, \$sort_sub)

``````    my \$label = [ qw/x y z/ ];
my \$data = [ [qw/3 2 1 4/],  [ qw/2 3 5 6/ ], [qw/4 5 1 6/] ];

my (\$new_label, \$new_data) = sort_by_other_arrayref(
\$label, \$data,
sub { # data line
my (\$r) = @_;
my (\$g, \$n, \$b, \$f) = @\$r;
my \$all = \$g+\$n+\$b+\$f;
return [ \$f/\$all, \$b/\$all ] },
sub {
my (\$x, \$y) = @_;
(\$x->[0] <=> \$y->[0]) or (\$x->[1] <=> \$y->[1]) },
);

dump(\$new_label, \$new_data);
#\$new_label ["z", "y", "x"]
#\$new_data  [[4, 5, 1, 6], [2, 3, 5, 6], [3, 2, 1, 4]]``````

## map_arrayref(\$arr, \$calc_sub, %option)

return_arrayref : 以arraryref格式返回结果，默认是以array格式返回

keep_source : 返回的结果中保留原始数据

calc_col : 指定数组中的项

``````    my \$data=[ 'haha', 'xx', 4 , 2, 3, 1, 'heihei'];
my @res = map_arrayref(
\$data,
\&calc_rate_arrayref,

calc_col => [ 2 .. 5 ],

return_arrayref => 1,
keep_source => 1,
);
# [ "haha", "xx", 4, 2, 3, 1, "heihei", 0.4, 0.2, 0.3, 0.1 ]
#keep_source => 1, return_arrayref=> 0 : ( "haha", "xx", 4, 2, 3, 1, "heihei", 0.4, 0.2, 0.3, 0.1 )
#keep_source => 0, return_arrayref=> 1 : [ 0.4, 0.2, 0.3, 0.1 ] ``````

## calc_percent_arrayref(\$arr, \$format);

``````    my \$format = "%.2f%%";
my @data = (3, 4, 1);
my \$r = calc_percent_arrayref(\@data, \$format);
dump(\$r);
#\$r : ["37.50%", "50.00%", "12.50%"]``````

## sum_arrayref

``````    my \$d = [ 1, 4, 3 ];
my \$r = sum_arrayref(\$d);
# \$r = 8``````

## mean_arrayref

``````    my \$d = [ 1, 4, 3 ];
my \$r = mean_arrayref(\$d);
# \$r = 2.66666667``````

## median_arrayref

``````    my \$d = [ 1, 4, 3 ];
my \$r = median_arrayref(\$d);
# \$r = 3``````

## uniq_arrayref

``````    my \$d = [ 1, 1, 4, 4, 3 ];
my \$r = uniq_arrayref(\$d,
# remember_key_order => 0,
);
# \$r = [ 1, 3, 4 ] ``````

## uniq_arrayref_cnt

``````    my \$d = [ 1, 1, 4, 4, 3 ];
my \$r = uniq_arrayref_cnt(\$d);
# \$r = 3``````

# OTHER FUNCTION

## conv_arrayref_to_hash

``````    my \$data=[ ['a','b',3],['e','f',6], ['a','f',9]];
my \$r = conv_arrayref_to_hash(\$data, [ 0, 1 ], 2,
# remember_key_order => 0,
);
# \$r =  { a => { b => 3, f => 9 }, e => { f => 6 } },``````

# BASE FUNCTION

## calc_rate

``````    my \$r = calc_rate(3, 4);
# \$r = 0.75``````

## format_percent

``````    my \$r = format_percent(0.675, "%.2f%%");
# \$r = '67.50%'``````

## calc_compare_rate

``````    my \$r = calc_compare_rate(4, 7);
# \$r = 0.75
my (\$r2, \$diff) = calc_compare_rate(4, 7);
# \$r2 = 0.75, \$diff = 3``````