设为首页 收藏本站
查看: 1760|回复: 0

[经验分享] perl数组以及qw

[复制链接]
YunVN网友  发表于 2018-9-15 10:38:49 |阅读模式
  perl允许你用任何标点符号作为定界符。常用的写法有:
  #!usr/bin/perl -w
  use strict;
  =pod
  my @arr = ("fred", "barney", "betty", "wilma", "dino");
  my @arr = qw(fred barney betty wilma dino);#同上,但更简洁,也更少键入
  my @arr = qw! fred barney betty wilma dino!;
  my @arr = qw/ fred barney betty wilma dino/;
  my @arr = qw# fred barney betty wilma dino#;
  my @arr = qw{ fred barney betty wilma dino};
  my @arr = qw[ fred barney betty wilma dino];
  =cut
  my @arr = qw< fred barney betty wilma dino>;
  for(my $i = 0; $i < @arr; $i++){
  print $arr[$i].&quot;\n&quot;;
  }
  输出:
  fred
  barney
  betty
  wilma
  dino
  互换两者的值
  #!usr/bin/perl -w
  use strict;
  my $a = &quot;apple&quot;;
  my $b = &quot;banana&quot;;
  =pod
  my $c = $a;
  $a = $b;
  $b = $c;
  =cut;
  ($a, $b) = ($b, $a);
  print &quot;变量a: &quot;.$a.&quot;\n变量b: &quot;.$b;
  输出:
  变量a: banana
  变量b: apple
  4.1 将数据放入列表和数组
  @array = (5,'apple',$x,3.1415926);
  print &quot;@array&quot;;#数组中的内容
  $count = @array;
  print &quot;\n&quot;.$count;#数组的个数
  print &quot;\n&quot;.$#array;#数组的最大索引
  输出:
  5 apple  3.1415926
  4
  3
  @array = qw(5 apple $x 3.1415926);
  print &quot;@array&quot;;#数组中的内容
  $count = @array;
  print &quot;\n&quot;.$count;#数组的个数
  print &quot;\n&quot;.$#array;#数组的最大索引
  输出:
  5 apple $x 301415926
  4
  3
  @array = (1..10);
  print &quot;@array&quot;;#数组中的内容
  $count = @array;
  print &quot;\n&quot;.$count;#数组的个数
  print &quot;\n&quot;.$#array;#数组的最大索引
  输出:
  1 2 3 4 5 6 7 8 9 10
  10
  9
  @array = (1..10,20..30);
  print &quot;@array&quot;;#数组中的内容
  $count = @array;
  print &quot;\n&quot;.$count;#数组的个数
  print &quot;\n&quot;.$#array;#数组的最大索引
  输出:
  1 2 3 4 5 6 7 8 9 10 20 21 22 23 24 25 26 27 28 29 30
  21
  20
  @cope = @origina ;
  $clean = ();
  @girls = qw(Merciz Jan Cindy);
  @boys = qw(Greg peter Bobby);
  @kids = (@girls, @boys);
  @family = (@kids,('Mile','Carol'), 'Alice');
  print &quot;@family\n&quot;;
  $count = @family;
  print $count.&quot;\n&quot;;
  print $#family;
  输出:
  Merciz Jan Cindy Greg peter Bobby Mile Carol Alice
  9
  8
  @family = qw(Merciz Jan Cindy Greg peter Bobby Mile Carol Alice);
  print &quot;@family\n&quot;;
  $count = @family;
  print $count.&quot;\n&quot;;
  print $#family;
  输出:
  Merciz Jan Cindy Greg peter Bobby Mile Carol Alice
  9
  8
  ($a , $b ,$c) = qw(apples oranges bananas);
  print $a.&quot;\n&quot;;
  print $b.&quot;\n&quot;;
  print $c.&quot;\n&quot;;
  输出:
  apples
  oranges
  bananas
  ($a,@fruit,$c) = qw(peaches mangoes grapes cherries);
  print $a.&quot;\n&quot;;
  print &quot;@fruit\n&quot;;
  print $c;
  输出:
  peaches
  mangoes grapes cherries
  ($t,$u,$v) = qw(partridge robin cardinal quail);
  print $t.&quot;\n&quot;;
  print $u.&quot;\n&quot;;
  print $v.&quot;\n&quot;;
  输出:
  partridge
  robin
  cardinal
  ($a,$b,$c,$d) = qw(squirrel woodchuck gopher);
  print $a.&quot;\n&quot;;
  print $b.&quot;\n&quot;;
  print $c.&quot;\n&quot;;
  print $d;
  输出:
  squirrel
  woodchuck
  gopher
  4.2 从数组中取出元素
  @trees = qw(oak cedar maple apple);
  print $trees[0].&quot;\n&quot;;
  print $trees[3].&quot;\n&quot;;
  $trees[4] = 'pine';
  print &quot;@trees&quot;;
  输出:
  oak
  apple
  oak cedar maple apple pine
  @trees = qw(oak cedar maple apple cherry pine peach fir);
  @conifers = @trees[5,6];
  print @trees[5,6].&quot;\n&quot;;
  print @conifers;
  输出:
  peach
  pinepeach
  4.2.1 寻找结尾
  @trees = qw(oak cedar maple apple cherry pine peach fir);
  print $#trees;
  输出:
  7
  4.2.2 关于上下文的详细说明
  @foo = qw( water pepsi coke lemonade );
  $a = @foo;
  $b = $#foo;
  print &quot;$a\n&quot;;
  print &quot;$b&quot;;
  输出:
  4
  3
  @mydata = qw( oats peas beans barley );if(@mydata)%7Bbr/>}@numbers;
print Dumper(@sorted);
  输出:
  $VAR1 = 2;
  $VAR2 = 4;
  $VAR3 = 5;
  $VAR4 = 7;
  $VAR5 = 8;
  use Data::Dumper;
  @numbers = qw(7 8 2 4 5);
  @sorted = sort { $a$b }@numbers;
  print Dumper(@sorted);
  输出:
  $VAR1 = '2';
  $VAR2 = '4';
  $VAR3 = '5';
  $VAR4 = '7';
  $VAR5 = '8';
  @lines = qw(I do not like green eggs and ham);
  print join(' ',reverse @lines);
  输出:
  ham and eggs green like not do I
  @lines = qw(I do not like green eggs and ham);
  print join(' ',reverse sort @lines);
  输出:
  not like ham green eggs do and I
  4.4练习:做一个小游戏
  #!/usr/bin/perl -w
  @words = qw( internet answers printer program);
  @guesses = ();
  $wrong = 0;
  $choice = $words[rand @words];
  $hangman = &quot;0-|--

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-583300-1-1.html 上篇帖子: Perl入门(七) Perl脚本的调试 下篇帖子: Perl 学习笔记-高级控制结构.unless控制结构
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表