2012-07-31 34 views
-1

根據以下腳本,試圖給出兩個輸入文件。 Test_DDD111_20120731.csv和DDD111.txt。 在一個文件夾中,此Test_DDD111 * .csv文件具有不同的日期。我想在這個腳本中只輸入當前的日期文件作爲輸入。在給出輸入文件名中的當前日期的情況下,perl腳本中的編譯錯誤

我將日期賦值爲$ deviationreportdate。但我收到錯誤,誰能幫我解決這個問題。

錯誤,我得到:

Scalar found where operator expected at subscriberdump.pl line 58, near "/Test_DDD(\d+)/${deviationreportdate}" 
     (Missing operator before ${deviationreportdate}?) 
     syntax error at subscriberdump.pl line 58, near "/Test_DDD(\d+)/${deviationreportdate}" 
     Execution of test.pl aborted due to compilation errors. 

#!/usr/bin/perl 

use strict; 
use warnings; 

use strict; 
use POSIX; 

my @array123; 

my $daysbefore; 
my %month=""; 

my ($f2_field, @patterns, %patts, $f2_rec); 


while (@ARGV) 

{ 
    my $par=shift; 

    if($par eq "-d") 

    { 

    $daysbefore=shift; 

    next; 
    } 
} 

sub getDate 
{ 
     my $daysago=shift; 

     $daysago=0 unless ($daysago); 

     my @months=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); 

     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = 

Localtime(time(86400*$daysago)); 

     # YYYYMMDD, e.g. 20060126 

     return sprintf("%d%02d%02d",$year+1900,$mon+1,$mday); 
} 

my $deviationreportdate=getDate($daysbefore-1); 

my $transactiondate=getDate($daysbefore); 

my $filename="output.txt"); 

open(OUTPUTFILE,"> /tmp/$filename"); 

for my $Test_file (<Test_DDD*${deviationreportdate}*>) 

{ 

    if ($Test_file =~ /Test_DDD(\d+)/${deviationreportdate}*) 

{ 

    my $file = "DDD$1.txt"; 

    my $ID="DDD$1"; 

    open AIN, "<$file" or die($file); 

    open BIN, "<$Test_file" or die($Test_file); 

    my %seen; 
} 
+0

請修正你的代碼。你想用正則表達式做什麼? – 2012-07-31 06:33:05

+0

好吧,給出語法錯誤的兩行是......語法錯誤。而且很難猜測你打算做什麼。也許你可以準確描述你期望的目標以及如果完成? – ysth 2012-07-31 07:02:43

+0

嗨,正如我前面提到的,在一個文件夾中,我有很多像Test_DDD111_20120731.csv和另一個文件DDD111.txt文件。我只是想給這兩個文件作爲輸入。我正在做一些比較。該比較腳本工作正常。但我希望該腳本自動獲取當前日期文件並執行相同的過程。如果csv和txt文件中的DDD111都是相同的,那麼我想給那些文件輸入並且日期應該是當前日期。 – Mike 2012-07-31 07:11:29

回答

1

這個正則表達式是無效

$Test_file =~ /Test_DDD(\d+)/${deviationreportdate}*

你只能有修飾符的正則表達式的最後一個斜線後。我不確定你想要做什麼,否則我會爲你發佈正確的正則表達式。也許你認爲這個?

$Test_file =~ /Test_DDD(\d+)\/${deviationreportdate}*/

或本

$Test_file =~ /Test_DDD(\d+)${deviationreportdate}*/

+0

嗨John,這是我想要的,現在它的工作正常。一個小小的錯誤即將來臨,你可以建議爲什麼它會來。 #perl test.pl -d 1 在test.pl第10行的散列分配中元素的奇數個數。 DeviationReportdate :: 20120731 TransactionDate :: 20120730 – Mike 2012-07-31 07:38:14

+0

@Mike這是因爲:'my%month =「」 ;'試圖用空字符串初始化散列。沒有任何意義。無論如何,代碼中不會使用%月份。 – Schwern 2012-07-31 10:00:03

+0

雅我刪除了。現在它工作正常。非常感謝 – Mike 2012-08-03 08:24:29

相關問題