在Perl数组中添加和删除元素
Perl提供了许多有用的功能来添加和删除数组中的元素。您可能有一个问题是什么功能?到目前为止,您已经使用打印功能来打印各种值。类似地,存在各种其他功能或有时称为子例程,它们可用于各种其他功能。
的值推到数组的末尾。
并返回数组的最后一个值。
将数组的第一个值移开并返回,将数组缩短1并向下移动所有内容。
添加到数组的前面,并返回新数组中的元素数。
示例
#!/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
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短