Here's what your instructor may be hinting at. Let's use the re-formatting of AppleFritter:

sub listprimes { my $n = shift; my $i = 0; my $answer = ""; while($primes[$i] <= $n) { $answer .= " $primes[$i]"; $i++; } return $answer; }
The while-loop is stepping through all the elements of  @primes and appending the element to a string while that element is less than or equal to the  $n limit. When an element of  @primes is encountered that is greater than the  $n limit, the while-loop terminates.

Another way to step through (or "iterate over") each element of an array is with a foreach loop (which can be shortened to for in all cases). This use of a for-loop is a common Perl idiom. Inside the for- or foreach-loop, you can decide to either append the element to the string or to exit the loop. (This doesn't use push; we'll get to that presently.) Something like:

for my $element (@primes) { if ($element <= $n) { $answer .= " $element"; } else { last; } }
or maybe:
for my $element (@primes) { last if $element > $n; $answer .= " $element"; }
See the last loop control built-in function. Note that the loop exit test in the second variation is explicitly  > whereas this had been only implicit before. Both of these approaches relieve you of the need to manage the  $i index variable.

How could you use push in all this? (I wouldn't bother; the code now seems simple enough.) In any event, maybe:

my @in_range; for my $element (@primes) { last if $element > $n; push @in_range, $element; } return " @in_range";
Note that the  $answer variable is no longer needed.

(But see NetWallah's reply for a very neat and concise implementation of this function.) (Update: But see my caveat.)

Update: Many small wording changes and additions of afterthoughts, and $string changed to $answer everywhere.


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: Perl program - I hope I can more understand my code by AnomalousMonk
in thread Perl program - I hope I can more understand my code by brianphan

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.