2017-09-15 118 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