2017-09-15 121 views
0

我在第一個函數內打印出數組並且它是有效的。我無法在第二個功能中打印出來。無法打印數組引用perl

錯誤消息:不能使用字符串( 「1」)作爲數組引用而 「嚴格參」 在使用中ConvertToVformat.pl線24

my $dir = cwd(); 
my $source_files = DetermineSourceFiles(); 
DetermineFileInfo($source_files); 

sub DetermineSourceFiles { 
    my @source_files; 
    opendir my $dh, $dir or die "Cant open $dir: $!"; 
    while (my $file = readdir($dh)) { 
     if ($file =~ /(.*?\d+.bin)/) { 
      push @source_files, $file; 
     } 
    } 
    closedir $dh; 
    return \@source_files; 
} 

sub DetermineFileInfo { 
    my $source_files = (@_); 
    foreach my $file (@$source_files) { 
     print "$source_files\n"; 
    } 
} 
+1

'my $ source_files =(@_);'分配給標量'$ source_files',因此處於標量上下文中;然後它使用數組中的元素數量,這裏是'1'。用'my($ source_files)= @_;'替換。幾天前,幾乎可以看到重複內容:[本文](https://stackoverflow.com/a/46232503/4653379)。 – zdim

+0

謝謝,解決了這個問題;另外,只需在$ source_files周圍放置圓括號就可以解決問題。我不明白移位的目的 – jelly

+0

我將它標記爲重複,因爲錯誤是完全相同的(即使代碼不同),並且上面的註釋使用相同的鏈接解決了它。如果有問題,請讓我知道。 – zdim

回答

0

使用

my $source_files = shift; 

而不是

my $source_files = (@_); 

更多信息,請參見documentation of shift