現在我正在嘗試使用屬於兩個不同輸入文件的某些數據進行一些比較。如何讀取很多Colums作爲文件中的數據並在Perl中正確存儲它們
第一個輸入文件如下所示:我有兩行和很多列。
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
我的主要目的是看這兩個文件,存儲必要的信息和DATAS的陣列。如果時間(如一小時)和新條件匹配,那麼我們想要比較它的數據和它的閾值。如果數據大於閾值,我想保留這些數據,並將結果寫入另一個文件。例如,對於ne S1和小時14,CC0數據等於7並且閾值等於50.
迄今爲止ı寫了此代碼; (最後的編輯之一,克里斯的幫助)
#! /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");
}
}
}
}
}
我現在可以正確地獲得一個簡單的方式,所有的數據,但是當涉及到最終如果命令我的意思是,
if ($ cc[$i] > $threshold)
CC數組值與所有的閾值進行比較,而不僅僅是相關cc_type和小時的值。 第二個輸入文件包含響應cc_types的閾值。對於每個cc_type,關於小時有23個不同的值,所以我只想比較特定小時和cc_type。如何解決這個問題?
(當我想出了第一部分,我會通過增加對S2的另一閾文件添加相同的過程。)
我在Perl語言是一個新手,所以與此相關的任何答案讚賞。 在此先感謝。 此致敬禮。
感謝您的答覆,ı刪除substr命令並添加拆分命令以獲取所有數據ı需要。現在,我可以正確檢索所有數據,但如果其他命令不能正常工作ı不明白爲什麼? – evenstar
@evenstar你的代碼看起來像做了許多不必要的步驟 - 特別是分配給'my @ cc116 = split(',',$ satir [122]); my @ cc117 = split(',',$ satir [123]); ','等'。但是要回答你關於'if .. else'的問題,'if(@hour == @ time1)'應該和'if($ hour [0] == $ time1 [0])一起工作' –
現在它起作用了正確的,你能解釋爲什麼我們這樣使用,爲什麼數組元素[0] ?.順便說一下,我不能編輯所有輸入文件,但在第一個文件中,我有2行和136列,它不僅是一行。在第二個文件中有4列和很多行。我無法弄清楚如何以簡單的方式單獨獲得127列數據,這就是爲什麼我需要manuelly分配的分割命令。 – evenstar