Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Perl program - I hope I can more understand my code

by NetWallah (Canon)
on Oct 21, 2015 at 19:52 UTC ( [id://1145586]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: Perl program - I hope I can more understand my code
by AnomalousMonk (Archbishop) on Oct 22, 2015 at 03:57 UTC
    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
        Thanks so much. I am trying to rewrite this program by using the foreach and push function. I think I am not right, can hint me?
        #!/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 = ""; foreach $x ($primes[$i]<=$n) { push $x{$n} } sub random { my($a,$b) = @_; return int(rand($b-$a+1))+$a; } my $a = random(10,50); my $f = listprimes($a); print "$f\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found