复制内容到剪贴板
代码:
chapter4
1.
--------------------------------/home/confish/perl/subr
#!/usr/bin/perl -w
#a subroutine named total returns sum of numbers
#confish@ubuntu7.10
sub total
{
foreach $n(0..$#_)
{
$sum+=$_[$n];
}
$sum;
}
my@fred=qw{1 3 5 7 9};
my $fred_total=&total(@fred);
print"The total of \@fred is $fred_total.\n";
print"Enter some numbers on separate lines:\n";
my $user_total=&total(<STDIN>);
print"The total of those numbers is $user_total.\n";
--------------------------------/home/confish/perl/subr
2.
--------------------------------/home/confish/perl/suber
#!/usr/bin/perl -w
#use the subroutine in last program to get the sum of 1..1000
#confish@ubuntu7.10
sub total
{
foreach $n(0..$#_)
{
$sum+=$_[$n];
}
$sum;
}
@num=(1..1000);
$sum=&total(@num);
print"The sum of 1..1000 is $sum\n";
--------------------------------/home/confish/perl/suber
3.
--------------------------------/home/confish/perl/aver
#!/usr/bin/perl -w
#to print the number which is larger than the average
#in some numbers
#confish@ubuntu7.10
sub average
{
foreach $n(0..$#_)
{
$sum+=$_[$n];
}
$average=$sum/($#_+1);
}
sub above_average
{
@num=@_;
@aba=();
$av=&average(@num);
foreach $n(0..$#_)
{
if($_[$n]>$av)
{
push ( @aba,$_[$n]);
}
}
@aba;
}
my @fred=&above_average(1..10);
print"\@fred is @fred\n";
print"(Shuold be 6 7 8 9 10)\n";
my @barney=&above_average(100,1..10);
print"\@barney is @barney\n";
print"(Should be just 100)\n";
--------------------------------/home/confish/perl/aver
chapter5
1.
----------------------------------/home/confish/perl/tac
#!/usr/bin/perl -w
#a prog same as cat but reverse the string
#confish@ubuntu7.10
@ARGV=reverse @ARGV;
@a=reverse<>;
print @a;
----------------------------------/home/confish/perl/tac
2.
----------------------------------/home/confish/perl/20str
#!/usr/bin/perl -w
#a prog that print the strings as 20 words flush right
#confish@ubuntu7.10
@str=<STDIN>;
while($i!=5)
{
foreach(0..9)
{
print;
}
$i++;
}
print"\n";
foreach(@str)
{
printf "%21s",$_;
}
----------------------------------/home/confish/perl/20str
3.
----------------------------------/home/confish/perl/20strpro
#!/usr/bin/perl -w
#a prog print the strings as number usr apionted words flush right
#confish@ubuntu7.10
@str=<STDIN>;
while($i!=5)
{
foreach(0..9)
{
print;
}
$i++;
}
print "\n";
$num=shift @str;
chomp $num;
$conv="%".++$num."s";
foreach(@str)
{
printf $conv,$_;
}
----------------------------------/home/confish/perl/20strpro
chapter6
1.
-------------------------------------/home/confish/perl/hash
#!/usr/bin/perl -w
#hashs that print the name's family name
#confish@ubuntu7.10
$family_name{"fred"}="flintstone";
$family_name{"barney"}="rubble";
$family_name{"wilma"}="flintstone";
print "enter the given name please:\n";
chomp($gn=<STDIN>);
print "family name for $gn is $family_name{$gn}\n";
------------------------------------/home/confish/perl/hash
2.
------------------------------------/home/confish/perl/counts
#!/usr/bin/perl -w
#hashs that counts the appearance times of the word
#print sorted
#confish@ubuntu7.10
print "enter the word please:\n";
foreach(<>)
{
$counts{$_}++;
$counts{$_}.="\n";
}
sort %counts;
print %counts;
------------------------------------/home/confish/perl/counts [
本帖最后由 zhponly 于 2008-3-9 17:55 编辑 ]