NAME
Simple Stat on arrayref, like sum, mean, calc rate, etc
简单数据统计处理
DESC
传入 scalar num / arrayref,计算 sum 求和, mean 均值, rate 比例 等
å®ä¾åèxtåæ件夹FUNCTION
calc_rate_arrayref
my @data = (3, 4, 1);
my $r = calc_rate_arrayref(\@data);
dump($r);
#$r:[0.375, 0.5, 0.125]
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
将arrayref转换为hash,注意重复的key会被覆盖掉,数据可能变少
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