learning note on 12/27/2020

列表是标量的有序集合。 数组是存储列表的变量。其实二者的概念差不多。

列表的值不一定放在数组中,但是数组变量一定包含一个列表。

array:

  $fred[0] = "yabba";

  最后一个Index: $rocks[$#rocks$] = 'science';

list:
  (1,2,3)

  (1..5) is the same as (1,2,3,4,5)

  qw: 可以用任意的界定符;

    qw(one, two, three)

    qw{one, two, three}

  assign the values to the variables in the list

    ($fred, $battery) = ("kan", "Peng");

    ($fred, $battery) = ("kan"); // here $battery is undef;

  @ means all of the list or array

    @rocks = (a, b, c);

    @ting = (d, e, f);

    @bb = (@rocks, @ting); //a,b,c,d,e,f

  push (@array, 0);

    pop @array; // pop out the last element of the array;

    $b = pop (@array); // pop out the last element of the array and assign the last popped element to $b;

     shift and unshift:

    @array = qw #kan, yun,peng#;

    $m = shift(@array); // m is now kan; @array is now qw #yun, peng#;

    unshit (@array, cool); // @array is now : qw #kan, yun, peng, cool#;

  splice:

    4 elements:

      1. @array name

      2. start point of the array

      3. length of the array

      4. element we want to insert (this can be none, so 1.2.3 can delete part of the array);

      exp:

        @array = qw (kan, yun, peng, for, you);

        @removed = splice @array, 1, 2, qw(wilma); // now @removed is yun, peng ; array is kan, wilma, peng, for, you ;

    字符串中的数组内插:

      @kan = qw(kan, yun, peng);

      print "There is @kan here\n";

      //数组被内插到引号

      所以假如我们想在“”插入email, 需要对@ 进行转义。

  foreach 

    foreach $a (@rocks) {

    }

   foreach 循环后,变量仍然保持旧的值,而不是数组最后一个的值, 如果没有定义,那么他的值是undef

 

  默认变量:

    foreach (1..10) {

      print "I can count to $_";

    } // the default value for perl

    $_ =  "Kan, yun,peng\n";

    print; # default print $_;

my @rocks = qw #kan good yun cool peng handsome#;

while (my($index , $value) = each @rocks) {

  say "$index : $value";

}

  or

foreach $index (0..$#rocks) {

  print "$index : $rocks[$index]\n";

}

 

up to page 66 

 

 

 

 

 

    

 

 

  

posted @ 2020-12-28 15:31  John_K  阅读(62)  评论(0编辑  收藏  举报