简介
perl 列表追加元素
主要是通过push和unshift函数来实现。其中,push是追加到列表尾,unshift是追加到列表头。
perl列表删除元素
主要是通过pop和shift函数来实现。其中,pop是从列表尾删除一个元素,
shift是从列表头删除一个元素。
试验代码
#!/usr/bin/perl
print("create an array\n");
@array = ("second","third","fourth");
my $size = @array;
print "$size, \@array = @array\n";
print "\n";
print("add to tail\n");
$size = push(@array, "fifth");
print "$size, \@array = @array\n";
print "\n";
print("add to head\n");
$size = unshift(@array, "first");
print "$size, \@array = @array\n";
print "\n";
print("del from tail\n");
$del_item = pop(@array);
$size = @array;
print("delete item: $del_item\n");
print "$size, \@array = @array\n";
print "\n";
print("del from head\n");
$del_item = shift(@array);
$size = @array;
print("delete item: $del_item\n");
print "$size, \@array = @array\n";
print "\n";
运行截图
从运行结果可以看到,分别实现了头尾2端追加元素,及头尾2端删除元素。