当前位置:文档之家› Perl语言学习练习及参考答案

Perl语言学习练习及参考答案

#题2:#使用for循环打印出如下的字符。

# 1# 12# 123# 12345#*******************#Fw_Print_Step ($step++,"使用for循环打印出如下的字符。

11212312345");my $str= "";for (1..4) {$str= $str.$_;if ($_==4) {$str= $str.$_+1;}print " $str\n";}#*******************##题3:my $str1 = "abc";my $str2 = "efg";#将上述2个字符串连接起来,并输出合并后的字符串长度#*******************#Fw_Print_Step ($step++,"将上述2个字符串\"$str1\"和\"$str2\"连接起来,并输出合并后的字符串长度");my $str =$str1.$str2;my $str_length=length($str);print "新字串$str的长度为:$str_length";#*******************##题4:#以逆序方式打印出字符串包含的各个字符,如变量为"123456789"则输出为"9","8",..."2","1". my $str1="abc123def456";#*******************#Fw_Print_Step ($step++,"以逆序方式打印出字符串包含的各个字符,如变量为\"123456789\"则输出为\"9\",\"8\",...\"2\",\"1\".");my $str=$str1;print "以逆序方式打印出字符串\"$str1\"包含的各个字符:\n";for($length=length($str1); $length>0; $length--) {$sub_str=chop($str);if ($length>1) {print "\"$sub_str\",";} else {print "\"$sub_str\".";}}#*******************##题5:#分别使用for与while循环来计算1+2+3+...+100的值#*******************#Fw_Print_Step ($step++,"分别使用for与while循环来计算1+2+3+...+100的值"); print "用for循环计算1+2+3+...+100的值:\n ";my $result=0;for (1..100) {$result=$result+$_;}print "1+2+3+...+100=$result";print "\n用while循环计算1+2+3+...+100的值:\n ";my $result=0;my $num=1;while ($num<=100) {$result=$result+$num;$num++;}print "1+2+3+...+100=$result";#*******************##题6:#以逆序的方式打印出端口列表包含的成员口my @cmdArray = ("config", "int fa 0/1", "no shutdonw", "end");#*******************#Fw_Print_Step ($step++,"以逆序的方式打印出端口列表包含的成员口");for (my $start=$#cmdArray; $start>=0; $start--) {my $array=$cmdArray[$start];print "$array\n";}#*******************##题7:#使用foreach打印出Hash表的所有下标与值my %map = ('red', 0xff0000,'green', 0x00ff00,'blue',0x0000ff);#*******************#Fw_Print_Step ($step++,"使用foreach打印出Hash表的所有下标与值");foreach $capword (sort keys(%map)) {print ("$capword: $map{$capword}\n");}#while( ($key, $value) = Each (%Map)) {# Print "\N$key=$value;"}#*******************##题8:#使用正则匹配判断字符串是否包含error,若是打印提示信息。

my $str = "Error:Unknown command!";#*******************#Fw_Print_Step ($step++,"使用正则匹配判断字符串是否包含error,若是打印提示信息。

"); if ($str =~ /error/i) {print "The string include \"error\"";} else {print "The string no include \"error\"";}#*******************##题9:#按照如下的函数定义写出该函数#===================================================================== ==========## 函数名: SetPortGroup# 函数功能:将DUT_PORT划分为端口列表,每个列表的端口速率必须相同(根据fa/gi进行判断),且2<=列表端口个数<=5# 输入参数:DUT_PORT数组名# 返回信息:端口列表#===================================================================== ==========##设置DUT参与测试的端口数my @DUT_PORT;$DUT_PORT[0]=5;#设置DUT的端口,端口号以gi 0/1、gi 2/1等形式来表示$DUT_PORT[1]="fa 0/1";$DUT_PORT[2]="fa 0/2";$DUT_PORT[3]="fa 0/3";$DUT_PORT[4]="fa 0/4";$DUT_PORT[5]="fa 0/5";$DUT_PORT[6]="fa 0/6";$DUT_PORT[7]="fa 0/7";$DUT_PORT[8]="fa 0/8";$DUT_PORT[9]="fa 0/9";$DUT_PORT[10]="gi 0/10";$DUT_PORT[11]="gi 1/1";$DUT_PORT[12]="gi 1/2";$DUT_PORT[13]="gi 1/3";$DUT_PORT[14]="gi 1/4";$DUT_PORT[15]="gi 1/5";$DUT_PORT[16]="gi 1/6";$DUT_PORT[17]="gi 1/7";$DUT_PORT[18]="gi 1/8";#调用SetPortGroup函数实现对DUT_PORT的分组#*******************#Fw_Print_Step ($step++,"按照如下的函数定义写出该函数.#=======================================================## 函数名: SetPortGroup# 函数功能:将DUT_PORT划分为端口列表,每个列表的端口速率必须相同(根据fa/gi进行判断),且2<=列表端口个数<=5# 输入参数:DUT_PORT数组名# 返回信息:端口列表#========================================================#");sub SetPortGroup {my @dutPort=@_;my @portList="";#统计FA端口的数目my $fa_num=0;print "将DUT_PORT数组中的端口转换为列表的形式:\n";for ($i=1;$i<=$dutPort[0];$i++) {@portList=(@portList,"$dutPort[$i]");}for (0..$#portList) {print "$portList[$_]\n";}print "\n-------------------------------------------------------\n";print "判断每个列表的端口速率是否相同\n";for ($i=1;$i<=$#portList;$i++) {my $speed=$portList[$i];if ($speed=~/fa/i) {print "端口$portList[$i] 的速率为100Mbps\n";$fa_num++;} else {print "端口$portList[$i] 的速率为1000Mbps\n";}}print "\n-------------------------------------------------------\n";if ($#portList < 2) {print "FAIL:测试端口不少于2个";@portlist="";} elsif ($#portList > 5) {print "FAIL:测试端口不超过5个";@portlist="";} elsif ($fa_num==0||$#portList==$fa_num) {print "PASS:列表的端口速率相同.";} else {print "FAIL:列表的端口速率不相同";@portlist="";}return @portList;}SetPortGroup @DUT_PORT;==========================题1:比较变量int1与int2的大小,并打印出比较结果==========================变量int1=2,int2=10比较结果:2 < 10==========================题2:使用for循环打印出如下的字符。

相关主题