Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl错误:全局符号需要显式的包名_Perl_Compiler Errors - Fatal编程技术网

Perl错误:全局符号需要显式的包名

Perl错误:全局符号需要显式的包名,perl,compiler-errors,Perl,Compiler Errors,我有一个文件 #File content word1 -> word2 word3 -> word4 我需要把它放在两个不同的数组中 @array1 = word1, word3 @array2 = word2, word4 我的代码如下 my @mappings = `cat $file_name`; foreach my $map (@mappings) { $map =~ s/^\s+|\s+$//g; #Remove leading and traili

我有一个文件

#File content
   word1 -> word2
word3 -> word4
我需要把它放在两个不同的数组中

@array1 = word1, word3
@array2 = word2, word4
我的代码如下

my @mappings = `cat $file_name`; 
foreach my $map (@mappings) { 
    $map =~ s/^\s+|\s+$//g; #Remove leading and trailing spaces 
    next if ($map =~ /^#/); 
    my @Mainarray = split ('->',$map); 
    my @array1 = push(@array1,@Mainarray[0]); **#Error line**
    my @array2 = push(@array2,@Mainarray[1]); **#Error line**
    print("Array1: @array1\nArray2:@array2\n"); 
}
我得到这个错误:

Global symbol "@array1" requires explicit package name.
Global symbol "@array2" requires explicit package name.

有人能帮我一下吗。

你的方法是每次通过foreach循环重新定义
@array1
&
@array2
,并尝试将它们设置为包含未定义值(自身)的值。试试这个:

my @mappings = `cat $file_name`;
my @array1;
my @array2;
foreach my $map (@mappings) { 
  $map =~ s/^\s+|\s+$//g; #Remove leading and trailing spaces 
  next if ($map =~ /^#/); 
  my @Mainarray = split (/->/,$map); 
  push(@array1, $Mainarray[0]); 
  push(@array2, $Mainarray[1]);
  print("Array1: @array1\nArray2:@array2\n"); 
}

可能重复欢迎使用堆栈溢出。请尽快阅读这一页。请提供完整的代码;您几乎肯定在使用
use strict
使用警告您没有显示的内容。您也不会显示Perl在错误消息中生成的行号。请仔细阅读如何制作SSCCE()。我的代码中使用了strict。整个代码几乎有500-600行。所以我无法粘贴它。我刚刚更新了上面代码中的错误行,在错误行旁边写了一条小注释。这太好了!当它循环时,从未意识到重新定义的部分!谢谢。是的,当循环中有
my
时,每次都会重置变量。我现在确实看到了。谢谢使用
$Mainarray[0]
从数组中提取单个元素。同时
使用警告