Observe the use of the arrow (->) operator when dealing with references.
This example adds two matrices.
-------
#!/usr/bin/perl
@array1 = (1, 2, 3); @array2 = (4, 5, 6,7);
AddArrays ([EMAIL PROTECTED], [EMAIL PROTECTED]); # Passing the array by
reference
print "@array1 \n";
sub AddArrays {
my ($rarray1, $rarray2) = @_;
$len2 = @$rarray2; # Length of array2
print "$len2 \n";
for ($i = 0; $i < $len2 ; $i++) {
$rarray1->[$i] += $array2->[$i];
}
}
-------
alfred,
Khairul Azmi wrote:
Can't declare reference constructor in "my" at array.pl line 10, near ")="
Execution of array.pl aborted due to compilation errors.
On Tue, 4 Jan 2005 09:42:55 +0800, Renqilong <[EMAIL PROTECTED]> wrote:
On Tue, 4 Jan 2005 09:21:43 +0800
Khairul Azmi <[EMAIL PROTECTED]> wrote:
Hi,
I'm trying to write a function that could return two arrays.
sub func {
my @array1=(1,2,3,4);
my @array2=(a,b,c,d);
return (@array1, @array2);
====> return ([EMAIL PROTECTED],[EMAIL PROTECTED]);
}
my (@out1,@out2)=func();
====> my ([EMAIL PROTECTED],[EMAIL PROTECTED]) = func();
for (my $i=0;$i<@out1;$i++) {
print "$out1[$i]";
}
print "\n";
for (my $i=0;$i<@out2;$i++) {
print "$out2[$i]";
}
print "\n";
The program would store everything onto @out1 and leave nothing to @out2
1234abcd
I read something about passing by reference but does not found
anything on return by references.
Help please. Thanks in advance.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
--
Whatever you do will be insignificant,but
the important is you do it!
--
Alfred Vahau
IT Services - University of Papua New Guinea
PO Box 320 - University PO - NCD 134
Papua New Guinea
Ph. (675) 3267277 - Fax. (675) 3267187 - Mobile: (675) 6888926
email: [EMAIL PROTECTED]