前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >perl学习笔记(3)

perl学习笔记(3)

作者头像
py3study
发布2020-01-14 12:38:14
1K0
发布2020-01-14 12:38:14
举报
文章被收录于专栏:python3

条件结构: 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退出。

代码语言:javascript
复制
#!/usr/bin/perl -w  while(1){          print qq(Input exit/1/2/...: );          $s=<STDIN>;          chomp $s;          last if($s eq "exit");           if($s eq "1"){                  print qq(Input a: );                  $a=<STDIN>;                  chomp $a;                  if($a){                          print qq(true);                  }else{                          print qq(false);                  }          }          elsif($s eq "2"){                  print qq(Input a: );                  $a=<STDIN>;                  chomp $a;                  print qq(Input b: );                  $b=<STDIN>;                  chomp $b;                  if($a eq $b){                          print qq($a = $b);                  }elsif($a gt $b){                          print qq($a > $b);                  }elsif($a lt $b){                          print qq($a < $b);                  }else{                          print qq(error);                  }          }else{}          print "\n";  } 

通过这个程序可以学习到字符串比大小是从左边第一个字符开始比的,数字小于大写字母,大写字母小于小写字母。实验: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 掌握条件结构和循环控制后,就可以处理一般数学问题了。

这次就到这里。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档