[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Elementary question (I think)



Chris Sansom wrote:
> 
> Hi MacPerlers
> 
> This is really a general Perl question, but it happens to have cropped up
> while I'm testing a CGI script written with MacPerl.
> 
> Suppose I have an array @stuff, when can and can't I use this syntax:
> 
> for ($i = 0; $i <= @stuff, $i++) {
> ...
> }
> 
> I was under the impression that this was perfectly safe, but it seems to
> break my script. As soon as I change all those @'s in this type of
> construct to $# it works just fine.
> 


scalar(@stuff) is the number of elements in the array @stuff.  Because the
first index is 0, the last index is @stuff-1.  Your loop goes past the end
of the array.


You should use either

   for ($i = 0; $i < @stuff, $i++) {

or

   for ($i = 0; $i <= $#stuff, $i++) {


Ronald

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org