加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl学习笔记(3)

发布时间:2020-12-16 00:27:11 所属栏目:大数据 来源:网络整理
导读:条件结构: if(...){ ????? ...; }elsif(...){ ????? ...; }else{ ????? ...; } 数值关系运算符 ==,,,=,=,!= 字符串关系???? eq,gt,lt,ge,le,ne 逻辑运算 与,或||,非! 与and,或or,非not ? . 数字0为假 . 空的字符串""和字符串0为假 . 未定义值undef为假 . 其

条件结构:
if(...){
????? ...;
}elsif(...){
????? ...;
}else{
????? ...;
}

数值关系运算符 ==,>,<,>=,<=,!=
字符串关系???? eq,gt,lt,ge,le,ne
逻辑运算 与&&,或||,非! 与and,或or,非not
?

. 数字0为假
. 空的字符串""和字符串0为假
. 未定义值undef为假
. 其他东西均为真


循环结构:
while(...){
}

for(init;test;increment){
?...
}


特殊句法(可读性强):
print q(say hello!) if($meeting);

last指令可提前退出当前循环。
next指令可提前跳入当前循环的下一次判断。
redo指令可重复当前循环的当次执行。
语句前可用标签,帮助last/next/redo等指令,起到类似于goto的作用。
?

exit指令退出perl。

练习:

输入1测真假,输入2比大小,输入exit退出。

 
 
  1. #!/usr/bin/perl?-w ?
  2. while(1){ ?
  3. ????????print?qq(Input?exit/1/2/...:?); ?
  4. ????????$s=<STDIN>; ?
  5. ????????chomp?$s; ?
  6. ????????last?if($s?eq?"exit"); ?
  7. ?
  8. ????????if($s?eq?"1"){ ?
  9. ????????????????print?qq(Input?a:?); ?
  10. ????????????????$a=<STDIN>; ?
  11. ????????????????chomp?$a; ?
  12. ????????????????if($a){ ?
  13. ????????????????????????print?qq(true); ?
  14. ????????????????}else{ ?
  15. ????????????????????????print?qq(false); ?
  16. ????????????????} ?
  17. ????????} ?
  18. ????????elsif($s?eq?"2"){ ?
  19. ????????????????print?qq(Input?a:?); ?
  20. ????????????????$a=<STDIN>; ?
  21. ????????????????chomp?$a; ?
  22. ????????????????print?qq(Input?b:?); ?
  23. ????????????????$b=<STDIN>; ?
  24. ????????????????chomp?$b; ?
  25. ????????????????if($a?eq?$b){ ?
  26. ????????????????????????print?qq($a?=?$b); ?
  27. ????????????????}elsif($a?gt?$b){ ?
  28. ????????????????????????print?qq($a?>?$b); ?
  29. ????????????????}elsif($a?lt?$b){ ?
  30. ????????????????????????print?qq($a?<?$b); ?
  31. ????????????????}else{ ?
  32. ????????????????????????print?qq(error); ?
  33. ????????????????} ?
  34. ????????}else{} ?
  35. ????????print?"n"; ?
  36. }?

通过这个程序可以学习到字符串比大小是从左边第一个字符开始比的,数字小于大写字母,大写字母小于小写字母。实验:2<H<h,1506<Happy,Happy<happy,Happy<h。

[abc@localhost tmp]$ vi f31.pl
[abc@localhost tmp]$ perl f31.pl
Input exit/1/2/...: 2
Input a: H
Input b: h
H < h
Input exit/1/2/...: 2
Input a: 2
Input b: H
2 < H
Input exit/1/2/...: 2
Input a: 1506
Input b: Happy
1506 < Happy
Input exit/1/2/...: 2
Input a: Happy
Input b: happy
Happy < happy
Input exit/1/2/...: 2
Input a: Happy
Input b: h
Happy < h
Input exit/1/2/...: exit
?

[abc@localhost tmp]$ perl f31.pl
Input exit/1/2/...: 1
Input a: 0
false
Input exit/1/2/...: 1
Input a:
false
Input exit/1/2/...: 1
Input a: aaaaa
true
Input exit/1/2/...: exit

掌握条件结构和循环控制后,就可以处理一般数学问题了。

这次就到这里。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读