in reply to Huge simple problem

Never mind. I see my problem now. Duh! The <= should be <. And to think that I spent over an hour on that.

Replies are listed 'Best First'.
Re^2: Huge simple problem
by toolic (Bishop) on Aug 06, 2009 at 01:07 UTC
    Perhaps the Basic debugging checklist would help shorten the debug cycle in the future, particularly tips #1 and #2:

    #1 use warnings; would have spewed many warnings which may have lead you to your problem.

    #2 print "i = $i\n"; inside your for loop

Re^2: Huge simple problem
by almut (Canon) on Aug 06, 2009 at 02:05 UTC
    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...)

      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.