sub calls

# Routine to Process an array element
### Called as &LineData(#);
sub LineData {
$I1 = [at] _ ;
$_ = $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);
[at] _ does contain the value but $I1 still maintains a 1. even when I try to
force $I1 = [at] _ ; in the debugger I still show a 1 for the $I1.

So what I am missing?

Frank
NC
FJRussonc [ So, 15 Januar 2006 23:47 ] [ ID #1142639 ]

Re: sub calls

Frank J. Russo wrote:
> # Routine to Process an array element
> ### Called as &LineData(#);
> sub LineData {
> $I1 = [at] _ ;
> $_ = $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);
> [at] _ does contain the value but $I1 still maintains a 1. even when I try to
> force $I1 = [at] _ ; 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, [at] _'s size is always one.
Change your assignment to one of the following:

my $l1 = shift [at] _; #remove and return the first element of [at] _
my $l1 = shift; #same thing - [at] _ is the default for shift()
my ($l1) = [at] _; #assigning in list context, rather than in scalar
context
my $l1 = $_[0]; #assign explicitly to first element.
Paul Lalli [ Mo, 16 Januar 2006 02:25 ] [ ID #1144086 ]
Perl » alt.perl » sub calls

Vorheriges Thema: and more questions
Nächstes Thema: Why does this not work?