Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Perl program - I hope I can more understand my code

by brianphan (Initiate)
on Oct 21, 2015 at 19:07 UTC ( [id://1145583]=perlquestion: print w/replies, xml ) Need Help??

brianphan has asked for the wisdom of the Perl Monks concerning the following question:

#!/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";
When I run this code, it will pickup the random number. I do not understand the code much, can explain for me. And hint for me if I want to rewrite the code which use the foreach and push funtion

Update

#!/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 @answer = (); foreach $a(@prime) { if($a<=$n){push @answer,$a;} 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";
I rewrite this program by using the foreach and push function. But it is not run, please help me to fix it

Replies are listed 'Best First'.
Re: Perl program - I hope I can more understand my code
by AppleFritter (Vicar) on Oct 21, 2015 at 19:54 UTC

    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?

      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?
Re: Perl program - I hope I can more understand my code
by NetWallah (Canon) on Oct 21, 2015 at 19:52 UTC
    The random() sub returns a random number betweent the values requested.

    The listprimes() can be rewritten as:

    sub listprimes { my ($n) = @_; return join " ", grep {$_ <= $n} @primes; }
    Which hopefully clarifies it.

            The best defense against logic is ignorance.

      return join " ", grep {$_ <= $n} @primes;

      This use of grep will iterate over the entire list of  @primes even after the  $n limit is encountered. This is not a problem for a list of the first fifteen primes, but might be for the first 15 million primes! One way to mitigate this overhead would be with the List::MoreUtils::last_index() function:

      c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; use List::MoreUtils qw(last_index); ;; my @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47) +; print qq{primes: @primes}; ;; sub listprimes { my ($n) = @_; ;; my $last_i = last_index { $_ <= $n } @primes; ;; return join ' ', @primes[ 0 .. $last_i ]; } ;; sub random { my ($lo, $hi) = @_; return $lo + int rand $hi - $lo + 1; } ;; for (1 .. 5) { my $r = random(10, 50); my $f = listprimes($r); print qq{r == $r: $f}; } " primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 r == 50: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 r == 32: 2 3 5 7 11 13 17 19 23 29 31 r == 18: 2 3 5 7 11 13 17 r == 47: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 r == 25: 2 3 5 7 11 13 17 19 23


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

        Alternatively, also using List::Util, but now use first:

        use List::Util qw(first); my @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47); sub listprimes { my $n = shift; my @foo; first { !($_ < $n ? push @foo, $_ : 0) } @primes; @foo; } sub random { my ($lo, $hi) = @_; return $lo + int rand $hi - $lo + 1; } for (1 .. 5) { my $r = random (10, 50); my @f = listprimes ($r); say "r == $r: @f"; }

        Enjoy, Have FUN! H.Merijn
Re: Perl program - I hope I can more understand my code
by Laurent_R (Canon) on Oct 21, 2015 at 23:31 UTC
    I would suggest that indenting correctly your code would help you understanding it.

    And I am really dead serious about that. The compiler does not care about code indentation, but you and I do. Really. Consistent indentation is not an option, it is not only necessary for comprehension, it is actually essential.

      Sometimes it's necessary to be able to see the question in order to be able to see the answer.


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1145583]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-24 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found