in reply to Re: While.For loop noob query
in thread While.For loop noob query

Hi Roy, Quick follow up if you are still in here..What is this doing?
for (@lines[0..($#lines - 1)])
it's kind of hard for me to see what's going on..Would you mind translating somewhat so I can adjust to my particular needs, if necessary. Thanks again

Replies are listed 'Best First'.
Re^3: While.For loop noob query
by Roy Johnson (Monsignor) on Oct 06, 2005 at 15:17 UTC
    It's going through all the elements of @lines except the last one. $#lines is the index of the last element of @lines. So 0..($#lines - 1) is therefore the range from zero to index of the next-to-last element. So @lines[0..($#lines - 1)] is a slice of all but the last element.

    Caution: Contents may have been coded under pressure.
Re^3: While.For loop noob query
by ikegami (Patriarch) on Oct 06, 2005 at 15:20 UTC

    $#lines is the index of the last elements of the array @lines. Let's say you have 10 lines. Their indexes would be 0 through 9, so $#lines would be 9.

    @lines[ LIST ] is an array slice. It returns a list formed from the elements indexed by LIST. In our earlier example, @lines[0..($#lines - 1)] returns elements 0 through 8.

    The for here starts a foreach loop over each element of the returned list. In other words, it loops over all up the last element of @lines. Since no variable name is specified, $_ will hold the value for the current iteration.

    I think $#array and array slices are documented in perldata. Loops are documented in perlsyn.