Arrays 如何在多个数组中添加所有数字,并在Perl中写入另一个数组
实际上,在本作业中,我需要将12个输入文件数据合并到一个输出文件中。每个像这样的输入文件Arrays 如何在多个数组中添加所有数字,并在Perl中写入另一个数组,arrays,perl,Arrays,Perl,实际上,在本作业中,我需要将12个输入文件数据合并到一个输出文件中。每个像这样的输入文件 ACME A 0 2 ACME A 1 0 ACME A 2 0 ACME A 3 0 ACME A 4 0 ACME A 5 0 ACME A 6 0 在要获取的输出文件ı中,所有相关数据如下 ********************************************************************** 2017-02-03 0 A
ACME A 0 2
ACME A 1 0
ACME A 2 0
ACME A 3 0
ACME A 4 0
ACME A 5 0
ACME A 6 0
在要获取的输出文件ı中,所有相关数据如下
**********************************************************************
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 18
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 0
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 0
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 1
这是我的全部代码
#! /usr/bin/perl -w
#compiler profilleri
use strict;
use warnings;
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
#dosya locationları
my $input_file="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_01.txt";
my $input_file1="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_06.txt";
my $input_file2="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_11.txt";
my $input_file3="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_16.txt";
my $input_file4="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_21.txt";
my $input_file5="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_26.txt";
my $input_file6="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_31.txt";
my $input_file7="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_36.txt";
my $input_file8="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_41.txt";
my $input_file9="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_46.txt";
my $input_file10="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_51.txt";
my $input_file11="C:/Perl64/output/sbc_cause_comp_intraday_2017-02-03_00_56.txt";
my $output_file="C:/Perl64/output/denemecik.txt";
#komutlar######
my $ne; my @cc_type; my @cc_count;
my @cc_count1; my @cc_count2; my @cc_count3; my @cc_count4; my @cc_count5;
my @cc_count6; my @cc_count7; my @cc_count8; my @cc_count9; my @cc_count10; my @cc_count11;
my @total; my $i;
my @count=0; my @count1=0; my @count2=0; my @count3=0; my @count4=0; my @count5=0; my @count6=0;
my @count7=0; my @count8=0; my @count9=0; my @count10=0; my @count11=0;
my $date='sbc_cause_comp_intraday_2017-02-03_00_01';
my $date1=substr($date,24,10);
my $hour=substr($date,35,1);
#print ($hour);
open INPUT, "< $input_file" or die "$0: open of $input_file failed, error: $! \n";
open INPUT1, "< $input_file1" or die "$0: open of $input_file1 failed, error: $! \n";
open INPUT2, "< $input_file2" or die "$0: open of $input_file2 failed, error: $! \n";
open INPUT3, "< $input_file3" or die "$0: open of $input_file3 failed, error: $! \n";
open INPUT4, "< $input_file4" or die "$0: open of $input_file4 failed, error: $! \n";
open INPUT5, "< $input_file5" or die "$0: open of $input_file5 failed, error: $! \n";
open INPUT6, "< $input_file6" or die "$0: open of $input_file6 failed, error: $! \n";
open INPUT7, "< $input_file7" or die "$0: open of $input_file7 failed, error: $! \n";
open INPUT8, "< $input_file8" or die "$0: open of $input_file8 failed, error: $! \n";
open INPUT9, "< $input_file9" or die "$0: open of $input_file9 failed, error: $! \n";
open INPUT10, "< $input_file10" or die "$0: open of $input_file10 failed, error: $! \n";
open INPUT11, "< $input_file11" or die "$0: open of $input_file11 failed, error: $! \n";
open OUTPUT, "> $output_file" or die "$0: open of $output_file failed, error: $! \n";
print OUTPUT ("**********************************************************************\n");
while ( defined ($_=<INPUT>) )
{
my $line = $_;
my ( $ne, $cc_type, $cc_count) = split ('\t',$line);
my $count=trim($cc_count);
print("$ne\n");
while ( defined ($_=<INPUT1>) )
{
my $line1 = $_;
my ( undef, undef, $cc_count1) = split ('\t',$line1);
my $count1=trim($cc_count1);
#print("$count1\n");
while ( defined ($_=<INPUT2>) )
{
my $line2 = $_;
my ( undef, undef, $cc_count2) = split ('\t',$line2);
my $count2=trim($cc_count2);
#print("$cc_count2\n");
while ( defined ($_=<INPUT3>) )
{
my $line3 = $_;
my ( undef, undef, $cc_count3) = split ('\t',$line3);
my $count3=trim($cc_count3);
#print("$cc_count3\n");
while ( defined ($_=<INPUT4>) )
{
my $line4 = $_;
my ( undef, undef, $cc_count4) = split ('\t',$line4);
my $count4=trim($cc_count4);
# print("$cc_count4\n");
while ( defined ($_=<INPUT5>) )
{
my $line5 = $_;
my ( undef, undef, $cc_count5) = split ('\t',$line5);
my $count5=trim($cc_count5);
#print("$cc_count5\n");
while ( defined ($_=<INPUT6>) )
{
my $line6 = $_;
my ( undef, undef, $cc_count6) = split ('\t',$line6);
my $count6=trim($cc_count6);
#print("$cc_count6\n");
while ( defined ($_=<INPUT7>) )
{
my $line7 = $_;
my ( undef, undef, $cc_count7) = split ('\t',$line7);
my $count7=trim($cc_count7);
#print("$cc_count7\n");
while ( defined ($_=<INPUT8>) )
{
my $line8 = $_;
my ( undef, undef, $cc_count8) = split ('\t',$line8);
my $count8=trim($cc_count8);
#print("$cc_count8\n");
while ( defined ($_=<INPUT9>) )
{
my $line9 = $_;
my ( undef, undef, $cc_count9) = split ('\t',$line9);
my $count9=trim($cc_count9);
#print("$cc_count9\n");
while ( defined ($_=<INPUT10>) )
{
my $line10 = $_;
my ( undef, undef, $cc_count10) = split ('\t',$line10);
my $count10=trim($cc_count10);
#print("$cc_count10\n");
while ( defined ($_=<INPUT11>) )
{
my $line11 = $_;
my ( undef, undef, $cc_count11) = split ('\t',$line11);
my $count11=trim($cc_count11);
#print("$cc_count11\n");
for( $i=0; $i< scalar @count; $i++ )
{
$total[$i] = $count[$i] + $count1[$i] + $count2[$i] + $count3[$i] + $count4[$i] + $count5[$i] + $count6[$i] + $count7[$i] + $count8[$i] + $count9[$i] + $count10[$i] + $count11[$i];
# print("@total\n");
}
print OUTPUT ("$date1 $hour $ne $cc_type $count $count1 $count2 $count3 $count4 $count5 $count6 $count7 $count8 $count9 $count10 $count11 $total\n");
# print("@total\n");
}
}
}
}
}
}
}
}
}
}
}
}
close OUTPUT;
close INPUT;
close INPUT1;
close INPUT2;
close INPUT3;
close INPUT4;
close INPUT5;
close INPUT6;
close INPUT7;
close INPUT8;
close INPUT9;
close INPUT10;
close INPUT11;
#/usr/bin/perl-w
#编译器profilleri
严格使用;
使用警告;
次修剪(元)
{
我的$string=shift;
$string=~s/^\s+/;
$string=~s/\s+$/;
返回$string;
}
#dosya locationları
我的$input\u file=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 01.txt”;
我的$input\u file1=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 06.txt”;
我的$input_file2=“C:/Perl64/output/sbc_原因_补偿日内_2017-02-03_00_11.txt”;
我的$input\u file3=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 16.txt”;
我的$input\u file4=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 21.txt”;
我的$input_file5=“C:/Perl64/output/sbc_原因_补偿日内_2017-02-03_00_26.txt”;
我的$input\u file6=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 31.txt”;
我的$input\u file7=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 36.txt”;
我的$input\u file8=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 41.txt”;
我的$input\u file9=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 46.txt”;
我的$input\u file10=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 51.txt”;
我的$input\u file11=“C:/Perl64/output/sbc\u原因\u补偿\u日内\u 2017-02-03\u 00\u 56.txt”;
my$output_file=“C:/Perl64/output/denemecik.txt”;
#科穆特拉######
我的$ne;我的@cc_类型;我的@cc_计数;
我的@cc_count1;我的@cc_count2;我的@cc_count3;我的@cc_count4;我的@cc_count5;
我的@cc_count6;我的@cc_count7;我的@cc_count8;我的@cc_count9;我的@cc_count10;我的@cc_count11;
我的@total;我的$i;
我的@count=0;我的@count1=0;我的@count2=0;我的@count3=0;我的@count4=0;我的@count5=0;我的@count6=0;
我的@count7=0;我的@count8=0;我的@count9=0;我的@count10=0;我的@count11=0;
我的$date='sbc_原因_公司日内_2017-02-03_00_01';
my$date1=substr($date,24,10);
my$hour=substr($date,35,1);
#打印(小时);
打开输入“<$INPUT_文件”或死“$0:打开$INPUT_文件失败,错误:$!\n”;
打开INPUT1“<$input\u file1”或死“$0:打开$input\u file1失败,错误:$!\n”;
打开INPUT2“<$input\u file2”或死“$0:打开$input\u file2失败,错误:$!\n”;
打开INPUT3“<$input\u文件3”或死“$0:打开$input\u文件3失败,错误:$!\n”;
打开INPUT4“<$input\u文件4”或死“$0:打开$input\u文件4失败,错误:$!\n”;
打开INPUT5“<$input\u文件5”或死“$0:打开$input\u文件5失败,错误:$!\n”;
打开INPUT6“<$input\u文件6”或死“$0:打开$input\u文件6失败,错误:$!\n”;
打开INPUT7“<$input\u文件7”或死“$0:打开$input\u文件7失败,错误:$!\n”;
打开INPUT8“<$input\u file8”或死“$0:打开$input\u file8失败,错误:$!\n”;
打开INPUT9“<$input\u file9”或死“$0:打开$input\u file9失败,错误:$!\n”;
打开INPUT10“<$input\u文件10”或死“$0:打开$input\u文件10失败,错误:$!\n”;
打开INPUT11“<$input\u file11”或死“$0:打开$input\u file11失败,错误:$!\n”;
打开输出“>$OUTPUT\u文件”或死“$0:打开$OUTPUT\u文件失败,错误:$!\n”;
打印输出(“*********************************************************************************************************************************\n”);
while(已定义($))
{
我的$line=$\ux;
我的($ne,$cc_类型,$cc_计数)=拆分('\t',$line);
my$count=修剪($cc\u count);
打印($ne\n);
while(已定义($))
{
my$line1=$\;
my(未定义,未定义,$cc_count1)=拆分('\t',$line1);
my$count1=修剪($cc\u count1);
#打印($count1\n);
while(已定义($))
{
my$line2=$\;
my(未定义,未定义,$cc_count2)=拆分('\t',$line2);
my$count2=修剪($cc_count2);
#打印($cc_count2\n);
while(已定义($))
{
my$line3=$\;
my(未定义,未定义,$cc_count3)=拆分('\t',$line3);
my$count3=修剪($cc\u count3);
#打印($cc_count3\n);
while(已定义($))
{
my$line4=$\;
my(未定义,未定义,$cc_count4)=拆分('\t',$line4);
my$count4=修剪($cc\u count4);
#打印($cc_count4\n);
while(已定义($))
{
my$line5=$\;
my(未定义,未定义,$cc_count5)=拆分('\t',$line5);
my$count5=修剪($cc_count5);
#打印($cc_count5\n);
while(已定义($))
{
my$line6=$\;
my(未定义,未定义,$cc_count6)=拆分('\t',$line6);
my$count6=修剪($cc_count6);
#打印($cc_count6\n);
while(已定义($))
{
my$line7=$\;
my(未定义,未定义,$cc_count7)=拆分('\t',$line7);
my$count7=修剪($cc\u count7);
#打印($cc_count7\n);
while(已定义($))
{
my$line8=$\;
my(未定义,未定义,$cc_count8)=拆分('\t',$line8);
my$count8=修剪($cc\u count8);
#打印($cc_count8\n);
while(已定义($))
{
我的$line9=$\;
my(未定义,未定义,$cc_count9)=拆分('\t',$line9);
my$count9=修剪($cc\u count9);
#打印($cc_count9\n);
sub sum { my $sum; $sum += $_ for @_; $sum }
my $sum = sum
@count,
@count1,
...
@count10,
@count11;
sub sum { my $sum; $sum += $_ for @_; $sum }
while (<>) {
chomp;
my @fields = split /\t/;
push @aoa, \@fields;
}
my $sum = sum map { @$_[4..15] } @aoa;
my $sum;
while (<>) {
chomp;
my @fields = split /\t/;
$sum += $_ for @fields[4..15];
}
#!/usr/bin/env perl
use strict;
use warnings;
#iterate the DATA filehandle below, line by line.
while ( <DATA> ) {
#split the row on whitespace.
my @row = split;
my $sum;
#iterate each field
foreach my $value ( @row ) {
#increment the sum by the value, if it's numeric.
if ( $value =~ m/^\d+$/ ) {
$sum += $value;
}
}
#print the sum for this row.
print $sum,"\n";
}
__DATA__
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 18
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw( sum );
my %counts;
while (my $line = <DATA>) {
my (@cols) = split ' ', $line;
my @numeric = map [$_, $cols[$_]], grep $cols[$_] =~ /^[0-9]+\z/, 0 .. $#cols;
for my $entry ( @numeric ) {
$counts{ 'VAR' . $entry->[0] } += $entry->[1];
}
}
use Data::Dumper;
print Dumper \%counts;
printf "Grand total = %d\n", sum values %counts;
__DATA__
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 18
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 18
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 18
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 18
2017-02-03 0 ACME A 0 2 4 43 4 4 25 4 3 26 4 4 18
$ perl s.pl
$VAR1 = {
'VAR7' => 215,
'VAR15' => 20,
'VAR8' => 20,
'VAR11' => 20,
'VAR14' => 20,
'VAR10' => 125,
'VAR12' => 15,
'VAR1' => 0,
'VAR4' => 0,
'VAR5' => 10,
'VAR6' => 20,
'VAR9' => 20,
'VAR16' => 90,
'VAR13' => 130
};
Grand total = 705