#----------------------------- foreach ($X .. $Y) { # $_ is set to every integer from X to Y, inclusive }
foreach $i ($X .. $Y) { # $i is set to every integer from X to Y, inclusive }
for ($i = $X; $i <= $Y; $i++) { # $i is set to every integer from X to Y, inclusive }
for ($i = $X; $i <= $Y; $i += 7) { # $i is set to every integer from X to Y, stepsize = 7 } #----------------------------- print "Infancy is: "; foreach (0 .. 2) { print "$_ "; } print "/n";
#----------------------------- use Roman; $roman = roman($arabic); # convert to roman numerals $arabic = arabic($roman) if isroman($roman); # convert from roman numerals #----------------------------- use Roman; $roman_fifteen = roman(15); # "xv" print "Roman for fifteen is $roman_fifteen/n"; $arabic_fifteen = arabic($roman_fifteen); print "Converted back, $roman_fifteen is $arabic_fifteen/n";
Roman for fifteen is xv
Converted back, xv is 15 #-----------------------------
#----------------------------- sub gaussian_rand { my ($u1, $u2); # uniformly distributed random numbers my $w; # variance, then a weight my ($g1, $g2); # gaussian-distributed numbers
$w = sqrt( (-2 * log($w)) / $w ); $g2 = $u1 * $w; $g1 = $u2 * $w; # return both if wanted, else just one return wantarray ? ($g1, $g2) : $g1; } #----------------------------- # weight_to_dist: takes a hash mapping key to weight and returns # a hash mapping key to probability sub weight_to_dist { my %weights = @_; my %dist = (); my $total = 0; my ($key, $weight); local $_;
foreach (values %weights) { $total += $_; }
while ( ($key, $weight) = each %weights ) { $dist{$key} = $weight/$total; }
return %dist; }
# weighted_rand: takes a hash mapping key to probability, and # returns the corresponding element sub weighted_rand { my %dist = @_; my ($key, $weight);
while (1) { # to avoid floating point inaccuracies my $rand = rand; while ( ($key, $weight) = each %dist ) { return $key if ($rand -= $weight) < 0; } } } #----------------------------- # gaussian_rand as above $mean = 25; $sdev = 2; $salary = gaussian_rand() * $sdev + $mean; printf("You have been hired at /$%.2f/n", $salary); #-----------------------------
#----------------------------- BEGIN { use constant PI => 3.14159265358979;
sub deg2rad { my $degrees = shift; return ($degrees / 180) * PI; }
sub rad2deg { my $radians = shift; return ($radians / PI) * 180; } } #----------------------------- use Math::Trig;
$radians = deg2rad($degrees); $degrees = rad2deg($radians); #----------------------------- # deg2rad and rad2deg defined either as above or from Math::Trig sub degree_sine { my $degrees = shift; my $radians = deg2rad($degrees); my $result = sin($radians);
#----------------------------- use PDL; # $a and $b are both pdl objects $c = $a * $b; #----------------------------- sub mmult { my ($m1,$m2) = @_; my ($m1rows,$m1cols) = matdim($m1); my ($m2rows,$m2cols) = matdim($m2);
for $i (range($m1rows)) { for $j (range($m2cols)) { for $k (range($m1cols)) { $result->[$i][$j] += $m1->[$i][$k] * $m2->[$k][$j]; } } } return $result; }
sub range { 0 .. ($_[0] - 1) }
sub veclen { my $ary_ref = $_[0]; my $type = ref $ary_ref; if ($type ne "ARRAY") { die "$type is bad array ref for $ary_ref" } return scalar(@$ary_ref); }
sub matdim { my $matrix = $_[0]; my $rows = veclen($matrix); my $cols = veclen($matrix->[0]); return ($rows, $cols); } #----------------------------- use PDL;
$a = pdl [ [ 3, 2, 3 ], [ 5, 9, 8 ], ];
$b = pdl [ [ 4, 7 ], [ 9, 3 ], [ 8, 1 ], ];
$c = $a x $b; # x overload #----------------------------- # mmult() and other subroutines as above
#----------------------------- sub commify { my $text = reverse $_[0]; $text =~ s/(/d/d/d)(?=/d)(?!/d*/.)/$1,/g; return scalar reverse $text; } #----------------------------- # more reasonable web counter :-) use Math::TrulyRandom; $hits = truly_random_value(); # negative hits! $output = "Your web page received $hits accesses last month./n"; print commify($output); Your web page received -1,740,525,205 accesses last month. #-----------------------------
printf "%d hour%s %s enough./n", $time, $time == 1 ? "" : "s", $time == 1 ? "is" : "are"; #----------------------------- printf "It took %d centur%s", $time, $time == 1 ? "y" : "ies"; #----------------------------- sub noun_plural { local $_ = shift; # order really matters here! s/ss$/sses/ || s/([psc]h)$/${1}es/ || s/z$/zes/ || s/ff$/ffs/ || s/f$/ves/ || s/ey$/eys/ || s/y$/ies/ || s/ix$/ices/ || s/([sx])$/$1es/ || s/$/s/ || die "can't get here"; return $_; } *verb_singular = /&noun_plural; # make function alias #----------------------------- use Lingua::EN::Inflect qw(PL classical); classical(1); # why isn't this the default? while (<DATA>) { # each line in the data for (split) { # each word on the line print "One $_, two ", PL($_), "./n"; } } # plus one more $_ = 'secretary general'; print "One $_, two ", PL($_), "./n";
#__END__ #fish fly ox #species genus phylum #cherub radius jockey #index matrix mythos #phenomenon formula #----------------------------- #One fish, two fish. # #One fly, two flies. # #One ox, two oxen. # #One species, two species. # #One genus, two genera. # #One phylum, two phyla. # #One cherub, two cherubim. # #One radius, two radii. # #One jockey, two jockeys. # #One index, two indices. # #One matrix, two matrices. # #One mythos, two mythoi. # #One phenomenon, two phenomena. # #One formula, two formulae. # #One secretary general, two secretaries general. #-----------------------------