Reputation: 587
I am trying to write a script for matrix multiplication. Its just a basic program but I am not able to figure it out about the following error :
Global symbol "@ref_mat1" requires explicit package name at multiplication.pl line 49.
Global symbol "@ref_mat2" requires explicit package name at multiplication.pl line 49.
Below is my script :
#!/usr/bin/perl -w
use strict;
my @mat1=(
[2,3,4],
[1,2,3],
[3,4,5]
);
my @mat2=(
[2],
[3],
[4]
);
my ($i, $j, $k);
my $r_product=[];
$r_product= mat_multiplication(\@mat1,\@mat2);
sub mat_multiplication
{
my ($ref_mat1,$ref_mat2)=@_;
my($mat1_row,$mat1_col)=total_rows_column($ref_mat1);
my($mat2_row,$mat2_col)=total_rows_column($ref_mat2);
for($i=0;$i<$mat1_row;$i++)
{
for($j=0;$j<$mat2_col;$j++)
{
my $sum=0;
for($k=0;$k<$mat1_col;$k++)
{
$sum=$sum+$ref_mat1[$i][$k]*$ref_mat2[$k][$j];
}
$r_product->[$1][$j]=$sum;
}
}
return $r_product;
}
sub total_rows_column
{
my($r_mat) =@_;
my $num_row=@{$r_mat};
my $num_col=@{$r_mat->[0]};
return($num_row,$num_col);
}
I searched for this problem and found one link
Explanation of 'global symbol requires explicit package name'
But still not able to resolve it. Please just have a look an let me know where am i doing mistake.
Thanks
Upvotes: 1
Views: 2814
Reputation:
$ref_mat1
and $ref_mat2
are references to arrays. In Perl if you want to access a reference to an array you cannot use $reference[$idx]
directly -- you have to use the ->
operator after the reference like this: $ref_mat1->[0]
.
Without it Perl thinks that $ref_mat1[0]
refers to an array @ref_mat1
which doesn't exist. Yes, both $var
and @var
can exist at the same time with differing content, see this example:
use strict;
use Data::Dumper;
my $abc = 42;
my @abc = (1, 2, 3);
print Dumper($abc), Dumper(\@abc);
Upvotes: 7