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?


In reply to Re: Perl program - I hope I can more understand my code by AppleFritter
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.