MSN: Phenzer@hotmail.com 欢迎加为好友讨论测试

perl中数组函数详解五:shift和unshift

上一篇 / 下一篇  2010-11-07 16:38:41 / 个人分类:脚本语言

   此文介绍的2个函数是在数组其实位置进行操作。
一、shift函数
 1.作用:移除数组的首元素,并返回该元素
 2.格式
      shift ARRAY;
      shift;
 3.实例:#shift
   @words=("tomataes","tomorrow","patatoes","phantom","Tommy");
   $item=shift @words;
   print "1.$item\n";
   print "2.@words";

4.结果
   1.tomataes
   2.tomorrow patatoes phantom Tommy

5.总结
 (1)数组首元素被弹出后,原数组大小相应减1
 (2)如果省略点参数ARRAY,则会移除ARGV数组的首元素;如果在子例程中则省略@_数组


二、unshift函数

 1.作用:在数组的起始位置追加元素
 2.格式:unshift(ARRAY,LIST)
 3.实例:#unshift
   @list=("Tom","Joe","Tonny","chris");
   unshift(@list,"Jack","Bob");
   print "@list";

4.结果
  Jack Bob Tom Joe Tonny chris

5.总结
  在数组末尾追加后,数组大小相应增大



TAG: 实例 应用 shift unshift

 

评分:0

我来说两句

Open Toolbar