Perl提供了许多有用的功能来添加和删除数组中的元素。您可能有一个问题是什么功能?到目前为止,您已经使用打印功能来打印各种值。类似地,存在各种其他功能或有时称为子例程,它们可用于各种其他功能。
| 序号 | 类型与说明 | 
|---|---|
| 1 | push @ARRAY,LIST将列表 的值推到数组的末尾。 | 
| 2 | pop @ARRAY弹出 并返回数组的最后一个值。 | 
| 3 | shift @ARRAY 将数组的第一个值移开并返回,将数组缩短1并向下移动所有内容。 | 
| 4 | 取消移位@ ARRAY,LIST将列表 添加到数组的前面,并返回新数组中的元素数。 | 
#!/usr/bin/perl
# create a simple array
@coins = ("Quarter","Dime","Nickel");
print "1. \@coins = @coins\n";
# add one element at the end of the array
push(@coins, "Penny");
print "2. \@coins = @coins\n";
# add one element at the beginning of the array
unshift(@coins, "Dollar");
print "3. \@coins = @coins\n";
# remove one element from the last of the array.
pop(@coins);
print "4. \@coins = @coins\n";
# remove one element from the beginning of the array.
shift(@coins);
print "5. \@coins = @coins\n";输出结果
这将产生以下结果-
1. @coins = Quarter Dime Nickel 2. @coins = Quarter Dime Nickel Penny 3. @coins = Dollar Quarter Dime Nickel Penny 4. @coins = Dollar Quarter Dime Nickel 5. @coins = Quarter Dime Nickel