|
楼主 |
发表于 2004-8-24 23:00:39
|
显示全部楼层
感谢,我做了一个小练习,是帮人统计数据的脚本。现在可以正常运行,但以我现在的水平一定有许多不完善的地方,希望大家批评。
#!/usr/bin/perl -w
@nums_src=("01.txt","02.txt","03.txt","04.txt","05.txt",
"06.txt","07.txt","08.txt","09.txt","10.txt","11.txt",
"12.txt","13.txt","14.txt");
#奇数文件名文件里的编号要计算其中所有编号对应值的和。
#偶数文件名文件里的编号要计算它前一个文件和此文件中所有
#编号对应值的和。
#------------------------------------------------------
open(RESULT,">results.xls");
print RESULT "\n"; #在开头输入一个空行
close RESULT;
########################## create @nums
@ori=();
open(ORI,"<src.txt");
while(<ORI>){
chomp $_;
@tmp=split(/\t/,$_,2);
push(@ori,@tmp);
}
close ORI;
%data=@ori;
@kys=keys(%data);
#---------------------------------------------
@ok=();
$f=1; #区别奇数和偶数文件名文件
foreach $num_src(@nums_src){ # every *.txt
open(NUM,"<./nums/$num_src");
@nums=();
while(<NUM>){
if($_=~/^(\d{3})\b/){
push @nums,$1;
}
}
close NUM;
############### calculate
@results=@tmp=();
foreach $num(@nums){ # each element in *.txt
$jurge=0;
foreach $k(@kys){
if($k=~/^($num)\b/){$jurge=1;last;}
}
if($jurge){
$y=0;
foreach $k(@kys){
if($k=~/^($num)\b/){
$y+=$data{$k};
}
}
push @results,$y;
}
else{push @results,"0"}
#-------------------------
}
push(@ok,@results);
####################################
################ output
open(RESULT,">>results.xls");
foreach $r(@results){print RESULT $r,"\n"}
$sum=0;
foreach $r(@results){$sum+=$r}
print RESULT $sum,"\n";
#------------------------------------
if($f==1){ #区别奇数和偶数文件名文件
$up_step=$sum;
}
else{
$sum=0;
foreach $r(@results){$sum+=$r}
$sumerize=$up_step+$sum;
print RESULT $sumerize,"\n";
$sumerize=0;
$up_step=0;
}
$f*=(-1); #区别奇数和偶数文件名文件
}
#----------------------------------------
$ok2=0;
foreach $okk(@ok){$ok2+=$okk}
print RESULT "\n",$ok2;
close RESULT; |
|