我认为,你可以坐在一张桌子上,这样,我就告诉你,你要求做什么,我认为什么是好的。
$name = "";
$data = {};
open(IN, "build.txt");
foreach my $line (<IN>){
if($line =~ /Re: (.*)
/) { # get building name
$name = $1;
$name =~ s/ /_/;
$data->{$name} = []; # link to empty array
} else {
# make a new list and return to a list
@{$data->{$name}} = (@{$data->{$name}}, $line); # add line to current building data
}
}
close IN;
#
# write on file for each
#
foreach my $name (keys %{$data}){
open(OUT, ">$name.txt");
foreach my $line (@{$data->{$name}}){
print OUT $line;
}
close OUT;
}
#
# or write into one file as a table
#
open(OUT, ">tabledata.txt");
foreach my $name (keys %{$data}){
# because the data was not filtered on import we filter it now
my $flag = 0;
my @data;
foreach my $line (@{$data->{$name}}){
if($line =~ /kWh/) {
$flag = 1;
} elsif($line =~ /^
$/){ # skip blanks
} elsif($flag == 1) { # skip the counters
$flag++;
} elsif($flag > 1) {
chomp($line);
@data = (@data, $line);
$flag = 1;
}
}
# print pretty rows
my $format = "%20s" . ("%10d" x @data);
print OUT sprintf($format, $name, @data) . "
";
}
close OUT;
这为每个建筑物提供建筑名称。 “A.txt”大楼的一个例子是:
Month
kWh
1
100
2
110
3
105
表格文档称为表格数据。
Building_A 100 110 105
Building_B 200 210 205