Turn the problem inside out: you want to print five groups of five numbers. That's the way you need to structure your nested loops.

You already have a random list of numbers so you can pull numbers off the front of the list 5 at a time using splice. So putting those together (with another small wrinkle) we get:

#!/usr/bin/perl use strict; use warnings; use List::Util 'shuffle'; my @numbers = shuffle(1 .. 50); for (1 .. 5) { my @group = splice @numbers, 0, 5; print "$_\n" for @group; print "\n"; }

The small wrinkle is the for @group "statement modifier" which is the nested loop that prints out the individual numbers in each group.

Note that to loop five time in Perl we can use for (1 .. 5) instead of the unwieldy and error prone C loop. The intent of the Perl loop is much clearer and much easier te get right.

Perl is the programming world's equivalent of English

In reply to Re: Generate a # between 1 and 50 in groups of 5 by GrandFather
in thread Generate a # between 1 and 50 in groups of 5 by Hayest

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.