Syntax Question

Posted on 2004-12-01
Last Modified: 2010-03-05
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++) {
Question by:Cosine_Consultants
    LVL 5

    Accepted 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.
    LVL 18

    Expert Comment

    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 ) {


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This video discusses moving either the default database or any database to a new volume.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now