Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to jump to specific point of a foreach iteration?

this is a minimal working example of how to skip iterations within a list of numbers:

#!/usr/bin/env perl

use strict;
use feature 'say';
use warnings FATAL => 'all';
use autodie ':default';

foreach my $n (0..29) {
    if ($n % 4 == 0) {
        $n += 5; # goes to $n+1, even though I increased $n to $n+5
        next;
    }
    say $n;
}

I cannot alter $n so that it jumps several iterations within the number.

That is, I want next to move 5 up, not 1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I skip multiple iterations of a foreach loop?

>Solution :

for ( my $n = 0; $n <= 29; ) {
   if ($n % 4 == 0) {
      $n += 5;
   } else {
      say $n;
      ++$n;
   }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading