in reply to Re: Perl program - I hope I can more understand my code
in thread Perl program - I hope I can more understand my code

Yes, It helps for me. Thanks you so much
  • Comment on Re^2: Perl program - I hope I can more understand my code

Replies are listed 'Best First'.
Re^3: Perl program - I hope I can more understand my code
by brianphan (Initiate) on Oct 21, 2015 at 23:11 UTC
    you know how to use the foreach loop and push function to rewrite this program. Can you hint me?

      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:  <%-{-{-{-<

        TIMTOWTDI is a very good principal but homework assignments often require use of a particular tool, for breadth of learning purposes. If you're going to do the assignment, for heaven's sake let's get a decent grade on the thing.

        Dum Spiro Spero
      listprimes() can be re-written even more efficiently using the Foreach loop.

      Push is not necessary to achieve the result, but here is how you could use it:

      sub listprimes { my ($n) = @_; my @primes_leq_n; for my $p (@primes){ # for and foreach are synonymous last if $p > $n; push @primes_leq_n, $p; } return join " ", @primes_leq_n; }

              The best defense against logic is ignorance.