sub calls

sub calls

am 15.01.2006 23:47:15 von FJRussonc

# Routine to Process an array element
### Called as &LineData(#);
sub LineData {
$I1 = @_ ;
$_ = $HEADERLINES[$I1];
# misc code #
1;
}

Problem: first time thru 1 is passed
&LineData(1);
and processed correctely.
Next pass a 6 is sent.
&LineData(6);
@_ does contain the value but $I1 still maintains a 1. even when I try to
force $I1 = @_ ; in the debugger I still show a 1 for the $I1.

So what I am missing?

Frank
NC

Re: sub calls

am 16.01.2006 02:25:36 von Paul Lalli

Frank J. Russo wrote:
> # Routine to Process an array element
> ### Called as &LineData(#);
> sub LineData {
> $I1 = @_ ;
> $_ = $HEADERLINES[$I1];
> # misc code #
> 1;
> }
>
> Problem: first time thru 1 is passed
> &LineData(1);
> and processed correctely.
> Next pass a 6 is sent.
> &LineData(6);
> @_ does contain the value but $I1 still maintains a 1. even when I try to
> force $I1 = @_ ; in the debugger I still show a 1 for the $I1.
>
> So what I am missing?

The fact that when you assign a scalar to an array, the scalar gets
that array's size, not the first element of the array. Since you're
only passing one element to the subroutine, @_'s size is always one.
Change your assignment to one of the following:

my $l1 = shift @_; #remove and return the first element of @_
my $l1 = shift; #same thing - @_ is the default for shift()
my ($l1) = @_; #assigning in list context, rather than in scalar
context
my $l1 = $_[0]; #assign explicitly to first element.