---
title: "perl的数组操作"
author: "standalone"
date: "2012-10-29T11:43:04Z"
tags: []
categories: ["编程语言"]
slug: "perl"
draft: false
lastmod: "2012-10-29T14:17:51Z"
id: 1707203
url: "https://standalone.iteye.com/blog/1707203"
---

**生成连续的一列数字或字母:**

my @numbers = (1..100);
my @chars   = (a..z);


**添加或者删除元素**

shift: 移除数组的第一个元素；
unshift: 添加一个元素到数组的最后；
push: 添加一个元素到数组的第一个元素；
pop：去除数组的最后一个元素。


|Functio|Definition|
|push(@array, Element)|	Adds to the end of an array|
|pop(@array)|Removes the last element of the array|
|unshift(@array, Element)|Adds to the beginning of an array|
|shift(@array)|	Removes the first element of an array|
|delete $array|	Removes an element by index number|


 **截取数组片断**

my @a=(1..10); 
my @b=@a; # @b = (2,3,4,5,9,10)
my @c=@a; # @c = (2,10)


 **添加删除元素
**

splice(@array, offset, length, $elem) 

my @a=(1..10);
splice(@a, 1, 0, 100) # @a=(1,100,2,3,4,5,6,7,8,9,10)
my @b=(1..10);
splice(@b, 1, 1, 99,100) # @b=(1,99,100,3,4,5,6,7,8,9,10)