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

First of all, let's format it nicely, shall we? (EDIT: I see you've added code tags now, good.)

#!/usr/bin/perl -w use strict; my @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47); sub listprimes { my $n = shift; my $i = 0; my $answer = ""; while($primes[$i] <= $n) { $answer .= " $primes[$i]"; $i++; } return $answer; } sub random { my ($a, $b) = @_; return int(rand($b - $a + 1)) + $a; } my $a = random(10, 50); my $f = listprimes($a); print "$f\n";

Now let's go through it from top to bottom. I have no idea how much Perl you know, so I'll just assume it's none; skip over anything you already know.

The first line is for your OS, and tells it to use /usr/bin/perl, the Perl interpreter, to execute your script. You also specify the -w flag to enable warnings; a word of advice there, it's generally preferable to use warnings instead.

Next, you load the strict module, which makes Perl a bit more nitpicky and helps you out by catching many common mistakes, typos etc. (Good idea!)

The third lines defines a new lexical (my) variable, an array (@) called @primes, and assigns to it a list containing what looks like the prime numbers below 50.

Coming up next, you have a subroutine (sub) definition, for a subroutine called listprimes. The subroutine body begins by shifting the first elements off of @_, an array holding the parameters passed to the subroutine that's also the default for shift and friends when no other array's specified as a parameter. The result is assigned to $n, a scalar ($), so that now holds the first parameter passed to the subroutine. Two more lexicals, $i and $answer, are declared and initialized to 0 and the empty string respectively.

Then you get a while loop. This keeps on executing the statements in the loop body while the loop condition ($primes[$i] <= $n) is true (i.e. evaluates to a true value) - while the $ith member of the previously-declared @primes array does not exceed $n, in other words.

While this is indeed true, you take said element, prepend a space (or, more precisely, interpolate it into a string that otherwise happens to contain just a space), and append the result to $answer. The period (.) is the string concatenation operator in Perl, and <op>= is the assigning form of many operators, just like += and friends in C if you're familiar with that. (If not, $result <op>= $operand is just syntactic sugar for $result = $result <op> $operand.) Also, still in the loop, $i is increased by one (the postfix increment operator's also straight from C).

So all in all, in this loop you're going through all the elements of @primes and appending them to $result, separated by spaces, until you find one that's greater than the value passed to the subroutine. And indeed that result is then finally returned to the caller.

After this there's another subroutine, random. Instead of using shift to access the arguments, you're now assigning the whole bunch (@_) to a two-element list, resulting in the first two parameters being assigned to $a and $b respectively. (The others are not assigned to anything in particular.) You then compute what is essentially a random number between $a and $b and return that.

In your main program, you first call random to obtain a random number between 10 and 50, and assign that to a lexical variable called $a. You then call listprimes and pass this random number, receiving a list (in string form, not a Perl list!) of primes below it, and store that in $f. Finally you print that, and that, as they say, is that.

Does this help?

Replies are listed 'Best First'.
Re^2: Perl program - I hope I can more understand my code
by brianphan (Initiate) on Oct 21, 2015 at 19:59 UTC
    Yes, It helps for me. Thanks you so much
      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:  <%-{-{-{-<

        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.