in reply to Re: push in for loop
in thread push in for loop

I was pretty sure that the array is being build on loopstart.

I got a list of clustered servers and want to load balance between them, but still re-use existing connections. The servers for the current try are within an array. I went through it and pushed all servers the script is connected to onto the list again to double their chances to be selected by the following random selection. I solved the issue using map, but I always like to understand why something isn't working. If it's a known limitation, it's not my fault and (now) I know not to try it :-)

Replies are listed 'Best First'.
Re^3: push in for loop
by jeffa (Bishop) on Dec 12, 2011 at 17:32 UTC

    I am glad you used map, which would be my recommendation for filtering an array. The reason the array is not being completely scanned before the for loop begins is due to memory optimization. You can always explicitly copy the array instead if needed, but it would be a code kludge to have to tell Perl not "build up the array" every time. (That's my take, anyway.)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)