Chinaunix首页 | 论坛 | 博客
  • 博客访问: 511170
  • 博文数量: 28
  • 博客积分: 3754
  • 博客等级: 中校
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-04 11:07
文章分类

全部博文(28)

文章存档

2012年(3)

2011年(3)

2010年(4)

2009年(3)

2008年(15)

分类:

2008-09-09 10:54:45

近期,老大让维护一些公司perl的程序,只好赶紧抓紧机会,好好学习一下。今天看了一下这两个函数的用法,记录一下。以下摘自

Perl array shift()&&unshift() function - Quick Tutorial

                     SHIFT
$ITEM = shift(@ARRAY);
Perl's shift() function is used to remove and return the first element from an array, which reduces the number of elements by one. The first element in the array is the one with the lowest index. It's easy to confuse this function with , which removes the last element from an array.
@myNames = ('Larry', 'Curly', 'Moe');
$oneName = shift(@myNames);
If you think of an array as a row of numbered boxes, going from left to right, it would be the element on the far left. The shift() function would cut the element off the left side of the array, return it, and reduce the elements by one. In the examples, the value of $oneName becomes 'Larry', the first element, and @myNames is shortened to ('Curly', 'Moe').

The array can also be thought of as a stack - picture of a stack of numbered boxes, starting with 0 on the top and increasing as it goes down. The shift() function would shift the element off the top of the stack, return it, and reduce the size of the stack by one.

@myNames = (
'Larry',
'Curly',
'Moe'
);
$oneName = shift(@myNames);

UNSHIFT
$TOTAL = unshift(@ARRAY, VALUES);
Perl's unshift() function is used to add a value or values onto the beginning of an array (prepend), which increases the number of elements. The new values then become the first elements in the array. It returns the new total number of elements in the array. It's easy to confuse this function with , which adds elements to the end of an array.
@myNames = ('Curly', 'Moe');
unshift(@myNames, 'Larry');
Picture a row of numbered boxes, going from left to right. The unshift() function would add the new value or values on to the left side of the array, and increase the elements. In the examples, the value of @myNames becomes ('Larry', 'Curly', 'Moe').

The array can also be thought of as a stack - picture of a stack of numbered boxes, starting with 0 on the top and increasing as it goes down. The unshift() function would add the value to the top of the stack, and increase the overall size of the stack.

@myNames = (
'Curly',
'Moe'
);
unshift(@myNames, 'Larry');
You can unshift() multiple values onto the array directly:
@myNames = ('Moe', 'Shemp');
unshift(@myNames, ('Larry', 'Curly'));
Or by unshift()-ing an array:
@myNames = ('Moe', 'Shemp');
@moreNames = ('Larry', 'Curly');
unshift(@myNames, @moreNames);

阅读(8845) | 评论(0) | 转发(0) |
0

上一篇:第13章 路由选择

下一篇:yum 配置

给主人留下些什么吧!~~