我有一個子程序,需要輸入一個字符串中的位置,並應返回在該位置找到的單詞。例如:如何獲取Perl正則表達式匹配變量的值與索引存儲在另一個變量?
use warnings;
use strict;
my $num=2;
my $val=getMatch($num);
sub getMatch {
my ($num)[email protected]_;
my $str='a b c';
$str=~ /(\S+)\s(\S+)/;
my $res;
eval "$res=\$$num";
return $res
}
但是這給了錯誤:
Use of uninitialized value $res in concatenation (.) or string at ./p.pl line 16.
(我試圖返回$i
其中i
是存儲在另一個變量的值。)
好像我忘了把一個斜槓'$ res'的面前:'EVAL「\ $水庫= \ $$ num「'..但也許有更簡單的方法來做到這一點? – 2014-12-04 08:50:00