For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2005 > References and subroutine ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author References and subroutine ?
Michael Gale

2005-04-22, 3:57 pm

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


Charles K. Clarkson

2005-04-22, 3:57 pm

Michael 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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com