in reply to Re: Huge simple problem
in thread Huge simple problem

I see my problem now...

Bonus points if you actually explain why using '<=' makes the loop run "endlessly" (until available memory is exhausted, that is)   :)

(Probably obvious to most monks, but maybe not to everyone...)

Replies are listed 'Best First'.
Re^3: Huge simple problem (reason)
by toolic (Bishop) on Aug 06, 2009 at 17:33 UTC
    Bonus points if you actually explain why
    Since the OP has not chimed in, I will make an attempt at an explanation.
    Probably obvious to most monks
    I freely admit that it was not obvious to me. I'm glad you prompted me to think about it further.

    Here goes. It is a case of modifying an array in a for loop. Before the for loop, the @searchTexts array contains 5 elements (indices: 0-4). The 1st 5 times though the loop, the array size remains constant at 5. Now, let's look at the end condition of the loop:

    $i <= @searchTexts +1 -1;

    which is just a slightly obfuscated form of:

    $i <= @searchTexts;

    Since the array is evaluated in scalar context, @searchTexts is the same as scalar @searchTexts, which returns the current size of the array. The 1st 5 times through the loop, the size is 5, and therefore the end condition is satisfied (the 5th time , $i is 4, which is less than or equal to 5). At the end of the 5th time through the loop, $i increments to 5, which is still less than or equal to 5. This causes the loop to be executed a 5th 6th time, which in turn causes the array to grow by 1 because of this line:

    @searchTexts[$i] =~ s/^\s+//;

    which resolves to this:

    @searchTexts[5] =~ s/^\s+//;

    From there on, the array continues to grow by one each time through the loop because the end condition is dependent on the array size.