发表于 2018-9-15 10:38:49

perl数组以及qw

  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 apple3.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.&quot;\n&quot;;
  print $trees.&quot;\n&quot;;
  $trees = 'pine';
  print &quot;@trees&quot;;
  输出:
  oak
  apple
  oak cedar maple apple pine
  @trees = qw(oak cedar maple apple cherry pine peach fir);
  @conifers = @trees;
  print @trees.&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;
  $hangman = &quot;0-|--
页: [1]
查看完整版本: perl数组以及qw