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#!/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";
Update
I rewrite this program by using the foreach and push function. But it is not run, please help me to fix it#!/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";
In reply to Perl program - I hope I can more understand my code by brianphan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |