Reputation: 309
I have a file that has
#File content
word1 -> word2
word3 -> word4
and I need to put this in 2 different arrays as
@array1 = word1, word3
@array2 = word2, word4
My code is as below
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");
}
I am getting this error:
Global symbol "@array1" requires explicit package name.
Global symbol "@array2" requires explicit package name.
Can someone please help me with this.
Upvotes: 3
Views: 6292
Reputation: 2350
the way you have it is redefining @array1
& @array2
each time through the foreach loop, and trying to set them equal to a value that contains an undefined value (itself). Try this:
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");
}
Upvotes: 3