2013-12-08 35 views
2

將兩段ASCII文本(一個L = long和一個S = short)分別讀入@arrayOne和@arrayTwo進行比較。以下子例程& analyze從smart.pl代碼獲取兩個數組引用,但通過perl -c smart.pl進行檢查時會引發錯誤。不幸的是我想不通爲什麼:Perl腳本中的奇數子程序編譯錯誤

68 sub analyse { 
69  my $arraysize ; my $arrLref ; my $arrSref ; my $item_L ; my $item_S ; my $value ; 
70 
71  $arrSref = shift ; $arrLref = shift ; 
72  $item_S = shift @{ $arrSref } ; 
73  $item_L = shift @{ $arrLref } ; 
74 
75  $arraysize = $#{ $arrSref } ; 
76  while ($arraysize > 0) { 
77   $value = ($item_S cmp $item_L) ; 
78   given ($value) { 
79    when (-1) { 
80     push (@mergedArray , $item_S) ; 
81     $item_S = shift @{ $arrSref } 
82    } 
83    when (0) { 
84     push (@mergedArray , $item_L) ; 
85     $item_S = shift @{ $arrSref } ; 
86     $item_L = shift @{ $arrLref } 
87    } 
88    when (1) { 
89     push (@mergedArray , $item_L) ; 
90     $item_L = shift @{ $arrLref } 
91    } 
92    default { &die } 
93   } 
94  } 
95 } 

編譯中止與下面的語句:

$ perl -c smart.pl 
    syntax error at smart.pl line 78, near ") {" 
    syntax error at smart.pl line 83, near ") {" 
    syntax error at smart.pl line 88, near ") {" 
    Global symbol "$item_L" requires explicit package name at smart.pl line 89. 
    Global symbol "$item_L" requires explicit package name at smart.pl line 90. 
    Global symbol "$arrLref" requires explicit package name at smart.pl line 90. 
    syntax error at smart.pl line 91, near "}" 
    smart.pl had compilation errors. 

也許別人有線索? THX提前-DrP-

+0

你有'while($ arraysize> 0){...'你不需要'$ arraysize = $#{$ arrSref};'倒數第二個'}',所以循環終止,或者只是'while($#{$ arrSref}){...'並忽略'$ arraysize = $#{$ arrSref};'一起? – Kenosis

+0

你使用的是哪個版本的perl?當聲明只在perl 5.18.0中可用... –

+0

你可以在這裏發佈perl -v –

回答

1

按照Perl documentation,爲了使用givenwhen,兩個條件必須滿足:

  1. 您需要use feature "switch";
  2. 您需要的Perl 5.10.1+

這應該解釋一下你看到的線路78,83是什麼,和88

[R在第89行和第90行看到的警告信息與使用use strict;有關,對這些警告的詳細解釋可參見here

+0

的輸出還是'use 5.10.1;'(或更高版本),它會自動啓用功能 – ysth

+0

Thx,但Perl v5.16.2說「使用開關已棄用」。所以我選擇了given-when語法。 -DrP- – DrParzival

+0

我編輯了我的帖子,以便更清楚。 –