Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: push in for loop

by jeffa (Bishop)
on Dec 12, 2011 at 17:04 UTC ( #943128=note: print w/replies, xml ) Need Help??


in reply to push in for loop

You can accomplish whatever it is you are desiring by using array indices instead:

my @problems = (1,2,3); for my $i (0 .. $#problems) { push @problems, $problems[$i] if $problems[$i]; } print for @problems;

Care to explain what it is you are trying to accomplish? :)

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)

Replies are listed 'Best First'.
Re^2: push in for loop
by NetWallah (Canon) on Dec 12, 2011 at 21:51 UTC
    Here is a more 'perlish' alternative to the 'c-style' loop:
    my @x=qw|0 1 2 3 4|; for my $s( @x[0..$#x] ){ # Slice = entire array $s and push @x, $s; }; print "@x\n"' #----Output--- #0 1 2 3 4 1 2 3 4

                "XML is like violence: if it doesn't solve your problem, use more."

Re^2: push in for loop
by Sewi (Friar) on Dec 12, 2011 at 17:28 UTC

    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 :-)

      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)
      

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://943128]
help
Chatterbox?
[marto]: pun tastic
[oiskuu]: some good ideas for turing testing here. Btw. has there been a "the best turing test questions" poll yet?
[LanX]: oiskuu -I don't think so, but I already know the winner!
[oiskuu]: Why would you say you don't think so, but you already know the answer?
[choroba]: also, do cheating answers to most popular turing test questions exist on bot forums?
[Your Mother]: : Describe in single words. Only the good things that come to your mind. About Your mother.
[marto]: you make up these questions yourself, or do they write 'em down for you?
[choroba]: Can humans count?
[oiskuu]: She Showers at YMCA?
[Your Mother]: marto++

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2017-12-18 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (487 votes). Check out past polls.

    Notices?