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

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++ ;

     for (my $i=$l[$#l]+1;$i<=$n;$i++) {
1 Solution
In perl all scalrs start with $: $i
Arrays, start with @: @l
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.
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 ) {

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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now