• Status: Solved
• Priority: Medium
• Security: Public
• Views: 184

# Syntax Question

What does the FOR "my \$i=\$l[\$#l]+1;" means  ?

my \$t     =shift @_ ;
my \$n     =shift @_ ;
my \$level =shift @_ ;
my @l     =@_ ;

#global @f,@h,@d,\$collsize

return       if (\$f[\$t] ne '') ;
\$collsize=0  if (\$level=0) ;

\$level++ ;

CANDIDATE :
for (my \$i=\$l[\$#l]+1;\$i<=\$n;\$i++) {
0
Cosine_Consultants
1 Solution

Commented:
Becuase member of arrays are scalars, they are referenced as such: \$l[0], \$l[1] (and NOT @l[0], @l[1]).
For an array named @l there is a special (scalar) variable that contains the number of the last valid index in the array and its name is \$#l.

So, "\$i=\$l[\$#l]+1"
\$#l =  is the number of the last valid index in array @l
\$l[\$#l] = the value of the @l array element in the \$#l place, actually the last value in the array.

Assign the last value in the array plus 1 to \$i

The "my" function declares the \$i variable to be private in the enclosing block of code.
0

Commented:
Hi Cosine_Consultants,
>     my \$t     =shift @_ ;
>      my \$n     =shift @_ ;
>      my \$level =shift @_ ;
>      my @l     =@_ ;

this is better written as

my(\$t, \$n, \$level, @l) = @_;

>     #global @f,@h,@d,\$collsize

>      return       if (\$f[\$t] ne '') ;
>      \$collsize=0  if (\$level=0) ;

You probably mean

\$collsize = 0 if \$level == 0;

>      \$level++ ;

>      CANDIDATE :
>      for (my \$i=\$l[\$#l]+1;\$i<=\$n;\$i++) {

More perlish would be

for my \$i ( \$l[-1]+1 .. \$n ) {

Cheers!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.