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

I've been looking and I can't quite figure out how to do this. I would like to print '\n' every nth element of an @array.

Replies are listed 'Best First'.
Re: print every nth element of an array.
by LanX (Saint) on Jun 06, 2013 at 01:57 UTC
    Homework?

    DB<122> @a = a..k => ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k") DB<123> $n=3,$x=0 => (3, 0) DB<124> grep {not ++$x % $n } @a => ("c", "f", "i")

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: print every nth element of an array.
by kcott (Archbishop) on Jun 05, 2013 at 23:15 UTC

    G'day rinaldi,

    Welcome to the monastery.

    The questions in your node title and content differ.

    "print every nth element of an array."

    Here's one way to print every nth element:

    $ perl -Mstrict -Mwarnings -e ' my @array = "a" .. "z"; my $n = 10; print $array[$_] for grep { ! (($_ + 1) % $n) } 0 .. $#array; ' jt
    "print '\n' every nth element of an @array"

    Here's how you might print a newline after every nth element (I'm assuming that's what meant unless you really did want to just print a string of newlines and nothing else):

    $ perl -Mstrict -Mwarnings -e ' my @array = "a" .. "z"; my $n = 10; print map { $array[$_] . (($_ + 1) % $n ? "" : "\n") } 0 .. $#arra +y; ' abcdefghij klmnopqrst uvwxyz

    -- Ken

Re: print every nth element of an array.
by davido (Cardinal) on Jun 05, 2013 at 22:20 UTC

    What programming language do you know well enough to accomplish this? It's probably very similar in Perl. Reading through perlintro will give someone who already has a minimal understanding of programming everything he needs.

    One way would be to use a C-style 'for' loop. ...probably not the most idiomatic solution, but certainly one that would be understood by almost anyone who has taken up programming, regardless of the language. Perl also has the modulus operator, and if statements for control of flow. It's ok to start with that subset of Perl that feels most comfortable to you.

    If you've been looking and trying to figure it out, you must have tried something, even if it didn't work out. Let's see what you've tried. It may be a pretty good start.


    Dave

      Y’know, in this case, I think that what I’d do is “a C-style for-loop,” for exactly the reason mentioned:   because it is “certainly one that would be understood by almost anyone who has taken up programming, regardless of the language.”   Someone (anyone ...) could someday encounter that piece of code, fairly-quickly guess what it was long-ago intended to do, and be correct.   If they now were tasked with changing that piece of code, they could once again do so with confidence.   And that, to me, pretty much trumps every other consideration.   “Shrink-wrap it... ship it...”

Re: print every nth element of an array.
by BrowserUk (Patriarch) on Jun 05, 2013 at 22:50 UTC

    @a = 0 .. 19;; $n = 4;; say "$_ => $a[ $_ ]" for map $_ * $n, 0 .. ( $#a / $n );; 0 => 0 4 => 4 8 => 8 12 => 12 16 => 16

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: print every nth element of an array.
by rjt (Curate) on Jun 06, 2013 at 01:14 UTC

    Yet another way to do this (which outperforms the above responses by my esteemed monk colleagues by at least 50% when output is ignored):

    for (my $i = 1; $i < $#a; $i += $n) { say $a[$i] }

    That said, loop performance will be obliterated by the I/O of say() (or print()), so if the C-like for loop construct doesn't look Perlish enough for your tastes, absolutely go with one of the other approaches, especially if your array is small.

Re: print every nth element of an array.
by Anonymous Monk on Jun 06, 2013 at 14:13 UTC
    I seem to be reading your question differently than the other monks, but I understand that you want to print your array in groups of n, with a blank line between.
    my $n = 5; # print every five lines my $i = 0; for my $item (@array) { print "$item\n"; $i++; print "\n" if $i % $n == 0; }
    Alternatively (≥v5.12 required):
    my $n = 5; while (my ($idx, $item) = each(@array)) { print $item; print "\n" if $idx % $n == $n - 1; }

      Thank you. so much. I hadn't thought to do it in that fashion.