Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Increment by 2 and by 1 in same loop.

by freekngeek (Acolyte)
on Apr 16, 2013 at 09:44 UTC ( [id://1028853]=perlquestion: print w/replies, xml ) Need Help??

freekngeek has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I am working on a script and I have to run a loop to print some lines and I need to increment some variables by 1 and some by 2. Is it possible to do that in a same loop ?

for(my $i = 0; $i<$size; $i += 2){ print FH "myMetal$i = dbCreatePath(cv list(\"@$metal_ref[$i]\" \" +drawing\") list(x1:y1 x2=(x1-(@$length_ref[$i]-1)):y1 ) @$width_ref[$ +i]) \n"; }

The thing is, that I also need to increment those x and y values by 1, but I am not able to do it. I can't run an extra loop, because it will give me multiple result that I don't need. So I hope if anyone of you could help me with this. Thank you.

Replies are listed 'Best First'.
Re: Increment by 2 and by 1 in same loop.
by hdb (Monsignor) on Apr 16, 2013 at 09:50 UTC

    Like this:

    my $x=0; my $size=5; for( my $i=0; $i<$size; $i+=2,$x++ ) { print "$i:$x\n"; }

    or

    my $x=0; my $size=5; for( my $i=0; $i<$size; $i+=2 ) { print "$i:",$x++,"\n"; }

      It's also possible to define (and initialize, if needed) two (or more) lexical loop variables entirely locally:

      for (my ($i, $n) = (1, 2); $i < $limit; $i += 2, ++$n) { do_something_with($i, $n); }
Re: Increment by 2 and by 1 in same loop.
by Anonymous Monk on Apr 16, 2013 at 09:53 UTC
    while( something ) { $anything++; $you += 2; $want += 11teen; }

      Definitely.

      Don’t make it clever .. always make it clear.   The odds are always good that someone (else) will come behind you and have to modify that piece of code.   Make it obvious what the code does, and easy to change it.

Re: Increment by 2 and by 1 in same loop.
by davido (Cardinal) on Apr 16, 2013 at 16:13 UTC

    Naturally there's always more than one way to do it.

    Your way (for loop):

    for ( # Lexical scope my($idx,$x,$y)=(0,0,0); # Initializer $idx<10; # Condition ++$idx, $x+=2, $y+=3 # Prep for next iteration. ) { print "$idx, $x, $y\n"; }

    ...which is exactly the same as (while loop):

    { # Lexical scope my( $idx, $x, $y ) = ( 0, 0, 0 ); # Initializer while( $idx < 10 ) { # Condition print "$idx, $x, $y\n"; } continue { # Prep for next iteration. ++$idx; $x += 2; $y += 3; } }

    It's up to you to decide which is more legible and maintainable. I think probably the while loop. Both loops are explicit in what they do, but the while loop, with its "continue" block is visually striking so that the fact that several variables are being incremented is not so easy to miss.

    Both are probably proof that it's possible to write C in Perl. ;) But I can't think of a more elegant approach at the moment. I guess that must say something about where I've been spending too much time.

    Update: By the way, if you pass the above code samples through B::Deparse, you'll find that they decompose almost identically:

    $ perl -MO=Deparse,-x9 ./mytest.pl # Decomposition of the "for" loop: while ($idx < 10) { print "$idx, $x, $y\n"; } continue { ++$idx, $x += 2, $y += 3 } # Decomposition of the "while" loop (essentially it's unchanged): { my($idx, $x, $y) = (0, 0, 0); while ($idx < 10) { print "$idx, $x, $y\n"; } continue { ++$idx; $x += 2; $y += 3; } } ./mytest.pl syntax OK

    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1028853]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-03-19 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found