我是perl的新手,需要你的帮助来构建一个逻辑。
我在目录中有10个文件,每个文件都有一些如下所示的数据。每个文件包含的行数取决于用户设置的数量。例如,如果有4个用户,那么将从服务器打印4行。
1405075666889,4044,SOA_breade,200,OK,Thread Group 1-1,text,true,623,4044
1405075666889,4041,SOA_breade,200,OK,Thread Group 1-1,text,true,623,4041
1405075666889,4043,SOA_breade,200,OK,Thread Group 1-1,text,true,623,4043
1405075666889,4045,SOA_breade,200,OK,Thread Group 1-1,text,true,623,4044
我想要一些逻辑应该在输出目录中创建单个文件,该文件应该包含10行
Min_Value, Max_Value, Avg_Value, User1, User2, User3......User4
在这种情况下,和第二行的相应值对应的值来自第二列。
Min_Value, Max_Value, Avg_Value, User1, User2, User3......User4
4.041,4.045,4.044,4.041,4.043,4.045
.
.
.
.
.
第10个文件数据
这是我的代码......但它正在运行我没有得到如何按顺序打印user1,user2 ...及其相应的值
my @soaTime;
my @soaminTime;
my @soamaxTime;
my @soaavgTime;
my $soadir = $Json_Time;
foreach my $inputfile (glob("$soadir/*Overview*.txt")) {
open(INFILE, $inputfile) or die("Could not open file.");
foreach my $line (<INFILE>) {
my @values = split(',', $line); # parse the file
my $time_ms = $values[1]/1000;
push (@soaTime, $time_ms);
}
my $min = min @soaTime;
push (@soaminTime, $min);
print $soaminTime[0];
my $max = max @soaTime;
push (@soamaxTime, $max);
sub mean { return @_ ? sum(@_) / @_ : 0 };
#print mean(@soaTime);
push (@soaavgTime, mean());
close(INFILE);
}
my $outputfile = $report_path."abc.txt";
open (OUTFILE, ">$outputfile");
print OUTFILE ("Min_Value,Max_Value,User1,User2,User3,User4"."\n"); # Prining the data
for (my $count = 0; $count <= $#soaTC; $count++) {
print OUTFILE("$soaminTime[0],$soamaxTime[0],$soaTime[0],$soaTime[1],$soaTime[2],$soaTime[3]"."\n" ); #Prining the data
}
close(OUTFILE);
请帮忙。
答案 0 :(得分:0)
use strict;
use List::Util qw( min max );
my $Json_Time="./test";
my $report_path="./out/";
my @soaTime;
my @soaminTime;
my @soamaxTime;
my @soaavgTime;
my @users;
my $maxusers;
my $soadir = $Json_Time;
foreach my $inputfile (glob("$soadir/*Overview*.txt")) {
open(INFILE, $inputfile) or die("Could not open file.");
my $i=0;
my @m_users;
my @m_soaTime;
foreach my $line (<INFILE>) {
my @values = split(',', $line); # parse the file
my $time_ms = $values[1]/1000;
push (@m_soaTime, $time_ms);
$i++;
push(@m_users, "User".$i);
}
push(@soaTime,\@m_soaTime);
if ($maxusers<$#m_users) {
@users=@m_users;
$maxusers=$#m_users;
}
my $min = min(@m_soaTime);
push (@soaminTime, $min);
my $max =max(@m_soaTime);
push (@soamaxTime, $max);
sub mean { return @_ ? sum(@_) / @_ : 0 };
push (@soaavgTime, mean());
close(INFILE);
}
my $outputfile = $report_path."abc.txt";
open (OUTFILE, ">$outputfile");
print OUTFILE "Min_Value,Max_Value,Avg_Value,".join(',',@users)."\n"; # Prining the data
for (my $count = 0; $count <= $#soaavgTime; $count++) {
print OUTFILE $soaminTime[$count].","
.$soamaxTime[$count].","
.$soaavgTime[$count].","
.join(',',@{$soaTime[$count]})
."\n"; #Prining the data
}
close(OUTFILE);