Perl 练习2

前端之家收集整理的这篇文章主要介绍了Perl 练习2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/usr/bin/perl -w
sub add{
	$num+=1;
	print "now num is $num \n";
}
&add;
&add;
&add;

#!/usr/bin/perl -w
#$arr1[0]="zz";
#$arr1[1]="aa";
#$arr1[2]="bb";
#$arr1[99]="cc";
#$startIndex=0;
#$endIndex=$#arr1;
#@names=qw#zhanzhao wangwu  qinfang#;
=this is a describe
@arr = qw#aaa bbb ccc #;
shift(@arr);
shift @arr;
#print $m."\n";
#print $n."\n";
unshift(@arr,"dd");
print @arr;
=cut
@arr=qw#aa bb cc dd ee#;
@removed=splice @arr,1,2,qw(" ff gg ");
print @removed," ",@arr,"\n";


#print @arr1;

#!/usr/bin/perl -w

sub sum_of_fred_and_barney{
	print "you called the sum_of_fred_and_barney function \n";
	return $fred+$barney;
	#print "return";
}
$fred=1;
$barney=2;
print &sum_of_fred_and_barney,"\n";
print 3*&sum_of_fred_and_barney,"\n";


#!/usr/bin/perl -w
#演示foreach的循环结构
=this is a descript
@arr=qw("aa bb cc dd");
foreach (1..10){
	print $_,"\n";
}
print "\n\n\n";
print "***************";
@nums=1..10;
@revNums=reverse(@nums);
print @revNums;
=cut

#排序sort
@names=qw(zhangsan liqiang 11 la wangwu);
@sortNames=sort(@names);
print "排序前:",@names,"\n";
print "排序后:",@sortNames,"\n";

#!/usr/bin/perl -w
my @names=qw(zhangzhao wangwu baidu wangyi qinfang);


sub getPosition{
	my($target,@arr)=@_;
	foreach (0..$#arr){
		if ($target eq @arr[$_]){
			return $_;
		}
	}
	-1;
}
print getPosition("baidu",@names);


#!/usr/bin/perl -w
#位置变量
print $ARGV[0],$ARGV[1],"\n";

#!/usr/bin/perl -w
#use strict;
use 5.085;

=pot
sub sumTest{
	state $sum=0;
	state @nums;
	foreach my $num $nun (@-){
		push @nums,$num;
		$sum+=$num;
	}
	say "sum is ",$sum,"\n";
}
=cut
print "zz";
#sumTest(1,2);
#sumTest(3..6);

#!/usr/bin/perl -w
@arr=qw(zhangsan wangwu aa bb);
foreach $item (0..$#arr){
	print "index is ",$item," value is ",$arr[$item],"\n";
}
#while(($index,$value)=each @arr){
	#print "index is ",$index,$value,"\n";
#}

#!/usr/bin/perl -w
=pod
#说明上下文的概念
@people=qw(zhangsan aa bb);
@len=@people;
$num=1+@len;
print $num;
=cut
#print 'zz';
chomp(@arr=<STDIN>);
print @arr;

#!/usr/bin/perl -w
%some_hash=('name','zhangzhao','age',23);
print %some_hash,"\n";
@arr=%some_hash;
print @arr;
print "****************","\n";
$newHash=reverse %some_hash;
print $newHash,"\n";

#!/usr/bin/perl -w
%myHash=("name"=>"zhanghzao","age"=>20);
@keys=keys %myHash;
@values=values %myHash;
print @keys,"\n";
print @values,"\n";

#!/usr/bin/perl -w 
#打印整个hash表
%myHash=("name"=>"zhangzhao","age"=>23);
while(($key,$value)=each %myHash){
	print "key : ",$key," value : ",$value;
	print "\n";
}

猜你在找的Perl相关文章