Perl练习

#!/usr/bin/perl
=this is a describe
$word="Hello "."World"."\n";
$newWord=$word x 4;
print $newWord; 
$num=3;
$num+=2;
print $num,"\n";
print "the answer is ",6*7,"\n";
print "num value is $num \n";


$line=<STDIN>;
if ($line eq "\n"){
	print "your input is a blank \n"
}
else{
	print "your input is $line \n"
}
=cut
chomp($text=<STDIN>);
#$text=<STDIN>;
print $text;


 

#!/usr/bin/perl 
$pi=3.1425926;
chomp($r=<STDIN>);
if($r < 0){
	$ans =0;
}else{
	$ans=2 * $pi * $r;
}
print "ans is $ans \n";


 

#!/usr/bin/perl -w
print "Enter the first num : ";
chomp($firstNum=<STDIN>);
print "Enter the second num: ";
chomp($secondNum=<STDIN>);
$ans=$firstNum * $secondNum;
print "the ans is $ans \n";


 

#!/usr/bin/perl -w
chomp(@lines=<STDIN>);
@afterLines=reverse(@lines);
#print @afterLines;
foreach $item (@afterLines){
	print $item,"\n";
}
print "**************";
print @afterLines[1];


 

#!/usr/bin/perl -w
@value=qw(zhangzhao wangfang liming baidu wangyi);
chomp(@indexs=<STDIN>);
foreach $item (@indexs){
	print @value[$item]," ";
}
print "\n";


 

#!/usr/bin/perl -w
chomp(@names=<STDIN>);
@afterSort=sort(@names);
foreach $item (@afterSort){
	print $item," ";
}
print "\n";


 

#!/usr/bin/perl -w
sub totalSum{
	$sum=0;
	foreach (@_){
		$sum+=$_;
	}
	$sum;
}

@nums=1..10000;
$ans=totalSum(@nums);
print $ans;


 

#!/usr/bin/perl -w

sub above_average{
	$sum=0;
	$ave=0;
	foreach (@_){
		$sum+=$_;
	}
	$len=@_;
	$ave=$sum/$len;
	foreach(@_){
		if($_ > $ave){
			print $_,"\n";
		}
	}

}
print above_average(1..30);


 

#!/usr/bin/perl -w


@arr=qw();
sub greet{
	$name=@_[0];
	push @arr,$name;
	$len=@arr;
	if( $len ==1 ){
		print "Hi $name ! you are the first one \n";
	}
	else{
		$pre=shift @arr;
		print "Hi $name ! $pre is also here \n";
	}
}
greet("zhangzhao");
greet("baidu");
greet("360");
greet("wangyi");


 

#!/usr/bin/perl -w


@arr=qw();
sub greet{
	$name=@_[0];
	push @arr,$name;
	$len=@arr;
	if( $len ==1 ){
		print "Hi $name ! you are the first one \n";
	}
	else{
		$pre = unshift @arr;
		print "Hi $name ! I have seen @arr is also here \n";
	}
}
greet("zhangzhao");
greet("baidu");
greet("360");
greet("wangyi");


 

#!/usr/bin/perl -w
$name="zhangzhao";
$name2 = reverse $name;
while(chomp($name=<STDIN>)){
	$reName=reverse $name;
	print $reName,"\n";
}


=opd
while($name=<>){
	$reName=reverse $name;
	print $reName,"\n";
}
=cut


 

#!/usr/bin/perl -w
my %myHash;
$myHash{"freb"}="filintston";
$myHash{"barney"}="rubble";
$myHash{"wilma"}="zhao";
chomp($name=<STDIN>);
print "value is ",$myHash{$name},"\n";


 

#!/usr/bin/perl -w
#统计各个单词出现的次数
my(@words,$myHash,$word);
chomp(@words=<STDIN>);
foreach $word (@words){
	$myHash{$word}+=1;
}
foreach $word (sort keys %myHash){
	print " $word has seen $myHash{$word} times \n";
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值