- 论坛徽章:
- 1
|
本帖最后由 rongchaogao 于 2013-12-27 19:54 编辑
- use strict;
- use warnings;
- my $out_out1 = "result.txt";
- open my $out1, '>', $out_out1 or die "Fail open $out_out1\n";
- print $out1 "Date\tserver\tgini\n";
- my (%hash,%freq,%amout,);
- my @files=glob "*.log";
- foreach my $file (@files)
- {
- $file=~m/_(\d{4})(\d{2})(\d{2})/;
- my $data="$1-$2-$3";
- my $in_in = "$file";
- open my $in, '<', $in_in or die "Fail open $in_in file\n";
- while(<$in>)
- {
- chomp;
- my @array=split /\|/;
- if($array[6]==15 or $array[6]==16)
- {
- $hash{$array[2]}{$array[5]}{$array[9]}=1;
- }
- else
- {
- next;
- }
- }
- foreach my $key1 (sort {$a<=>$b} keys %hash)
- {
- foreach my $key2 (sort {$a<=>$b} keys %{$hash{$key1}})
- {
- foreach my $key3 (sort {$a<=>$b} keys %{$hash{$key1}{$key2}})
- {
- $freq{$key1}{$key2}++;
- $amout{$key1}{$key2}+=$key3;
- }
- }
- }
- foreach my $key1 (sort {$a<=>$b} keys %freq)
- {
- my @origx=qw//;;
- foreach my $key2 (sort {$b<=>$a} keys %{$freq{$key1}})
- {
- push @origx,$amout{$key1}{$key2};
- }
- my $gini=calc_gini(\@origx,$data,$key1)
- }
-
- undef %hash;
- undef %freq;
- undef %amout;
- close $in;
- }
- close $out1;
-
- sub calc_gini
- {
- my $ginisum=0;
- my $sum=0;
- my $cout=@{$_[0]};
- for (my $i=1;$i<=$cout;$i++)
- {
- $ginisum=$ginisum+(2*$i-$cout-1)*$_[0]->[$i-1];
- $sum=$sum+$_[0]->[$i-1];
- }
- my $gini=$ginisum/($cout-1)/$sum;
- print $out1 "$_[1]\t$_[2]\t$gini\n";
- }
复制代码 |
|