现在我正在尝试与使用属于两个不同输入文件的某些数据进行比较。
第一个输入文件如下所示:我有两行和很多列。
id date1 time1 date2 time2 ne CC0 CC1 CC2 CC3 CC4... due to CC127
1 2016-09-26 14:13:56 2016-09-26 14:08:56 S1 7 1226 0 86 0
2 2016-09-26 14:13:56 2016-09-26 14:08:56 S2 8 1346 2 97 12
第二个输入文件如下所示:
ne type time threshold
S1 CC000 09 50
S1 CC000 10 50
S1 CC000 11 50
S1 CC000 12 50
S1 CC000 13 50
S1 CC000 14 50
我的主要目的是阅读这两个文件,在数组中存储必要的信息和数据。如果时间(以小时为单位)和ne条件匹配则ı想要比较其数据和阈值。如果数据大于阈值,则ı想要保留此数据并将其作为结果写入另一个文件。例如,对于ne S1和小时14,CC0数据等于7并且阈值等于50.
到目前为止,我写了这些代码; (最后一次编辑,在Chris的帮助下)
#! /usr/bin/perl -w
#compiler profilleri
use strict;
use warnings;
#dosya locationları
my $input_file="C:/Perl64/output/innput.txt";
my $s1_threshold="C:/Perl64/output/s1_threshold.txt";
#komutlar######
my $date; my $time; my $ne; my @hour; my @cc;
my $i=0; my $j=0;
open INPUT, "< $input_file" or die "$0: open of $input_file failed, error: $! \n";
while ( defined ($_=<INPUT>) )
{
my $line1 = $_;
my ( undef, $date, $time, undef, undef, $ne, @cc) = split (' ',$line1);
#print("$cc[16]\n");
my @time1= split(':',$time);
@hour=split(',',$time1[0]);
#print("@hour\n");
open THR, "< $s1_threshold" or die "$0: open pf $s1_threshold failed, error: $! \n";
while (defined($_=<THR>) )
{
my $line2=$_;
my ($ne1, $cc_type, $time1, $threshold ) =split(' ',$line2);
if( $hour[0] == $time1 && $ne eq $ne1 )
{
for ( $i=0;$i<128;$i++)
{
if ( $cc[$i] > $threshold )
{
# print("$cc[$i]\n");
}
}
}
}
}
现在ı可以通过简单的方式正确获取所有数据,但是当涉及到final if if命令时,我的意思是
if ( $ cc[$i] > $threshold )
将cc数组值与所有阈值进行比较,而不仅仅是相关cc_type和小时的值。 第二个输入文件包含响应cc_types的阈值。对于每个cc_type,相对于小时有23个不同的值,因此ı想仅比较特定小时和cc_type。怎么能解决这个问题?
(当ı找出第一部分时,ı将通过为S2添加另一个阈值文件来添加相同的程序。)
我是perl语言的新手,所以任何与此相关的答案都表示赞赏。 提前致谢。 问候。
答案 0 :(得分:0)
您正在尝试将所有值用作一个整数,这将永远不会起作用。您需要逐个获取这些值。你解析这些行的方式也是麻烦。你最好用这样的东西:
my ($id, $date1, $time1, $date2, $time2, $ne, $cc0, $cc1, $cc2, $cc3, $cc4) = split /\s+/, $sonuc;
现在,您可以单独使用$cc0, $cc1, ...
作为整数。
答案 1 :(得分:0)
更新将比较行更改为if ( $hour1 == $hour2 && $ne1 eq $ne2 )
并在my $i = ...
语句中移动if
。
如果我理解&#39;类型&#39;变量正确,(CC000
=&gt; 000
),然后这里更改的代码可能会做你需要的。
我没有使用substr
来获取数据,而是将split
字段放入变量中。
在第一个文件中,最后一个接收器@cc
获取输入行中的所有剩余列,(您说第一个文件中只有一行数据)。
如果只有一行,则不需要while
循环来读取数据。简单地说,请注意我如何将1行读入变量(split ' ', <$fh>
)。
由于您似乎不需要date1
和time1
,因此我将其分配给undef
。 (undef
此处只是您不想捕获的值的占位符。我本可以使用undef
作为第一个字段,但我已将其分配给$id
您不管怎么说都没用。)
另外,我使用了词法文件句柄,($fh
,$fh2
),而不是INPUT
和THR
,因为这是首选做法。我不能确切地说出它为什么是首选,但我认为它是在perl v 5.6中采用的。
我还使用了打开文件的3 argument,(文件句柄,模式,文件)模式。 (你使用了2参数,3 arg。是在perl ver.5.6中引入的。它是open
)的一种更安全的形式。
#!/usr/bin/perl
use strict;
use warnings;
my $input_file = 'file1';
my $s1_threshold="file2";
open my $fh, '<', $input_file
or die "$0: open of $input_file failed, error: $! \n";
my ($id, $date, $time1, undef, undef, $ne1, @cc) = split ' ', <$fh>;
close $fh or die "$0: close of $input_file failed, error: $! \n";
# get hour from time1
my $hour1 = substr $time1, 0, 2;
open my $fh2, '<', $s1_threshold
or die "$0: open pf $s1_threshold failed, error: $! \n";
while (<$fh2>) {
my ($ne2, $cc, $hour2, $threshold) = split;
if ( $hour1 == $hour2 && $ne1 eq $ne2 ) {
my $i = 0 + substr $cc, 2;
if ( $cc[$i] > $threshold )
{
print("$cc[$i]\n");
print ("match\n");
}
else
{
print("not match\n");
}
}
}
close $fh2 or die "$0: close pf $s1_threshold failed, error: $! \n";