Code Comments
Programming Forum and web based access to our favorite programming groups.Hello,
I have an array and I would like to pass it to a subroutine:
&printlist(\@array);
sub printlist
{
my $array_ref = (shift);
my $max;
my $maxnew = @{$array_ref};
for(my $i=0;$i < $maxnew; $i++){
print "\t\t\t\t\t";
if ( ($maxnew - $i) >= 10 ) {
$max = 10;
} else {
$max = $maxnew - $i;
}
for(my $n=0; $n <= $max; $n++) {
print "${$array_ref}[$i]\t";
$n++;
$i++;
}
print "\n";
}
}
I know the above works I just want to make sure I understand why,
I am passing the reference of an to the subroutine, which using (shift)
assigns the first scaler to my scaler array_ref. So now array_ref scaler
is equal to the passed scaler allowing me to use array_ref as a
reference.
Correct ?
Michael
Post Follow-up to this messageMichael Gale <mailto:michael.gale@pason.com> wrote: : I am passing the reference of an to the subroutine, which using : (shift) assigns the first scaler to my scaler array_ref. So now : array_ref scaler is equal to the passed scaler allowing me to use : array_ref as a reference. : : Correct ? Yes, except you're misspelling 'scalar'. Perl places a reference to @array into @_ when you call the subroutine. When you use 'shift', you remove the first item from @_ (which is an array reference) and set it to $array_ref. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.