{{tag> note:perl unshift shift push pop}}
---json
{
"title":"配列の操作(push/pop/shift/unshift)",
"description":"配列の操作(push/pop/shift/unshift)"
}
---
\\
==== 配列の操作(push/pop/shift/unshift) ====
\\
^ 関数 ^ 説明 ^
| unshift | 配列の前から追加 |
| push | 配列の後ろから追加 |
| shift | 配列の前から取り出す |
| pop | 配列の後ろから取り出す |
\\
use Data::Dumper;
my @a = (1, 2, 3, 4, 5);
print Dumper(@a); # [ 1 2 3 4 5 ]
=== unshift ===
print "\n";
print " unshift 9";
print "\n";
unshift(@a, 9); # 9 => [ 9 1 2 3 4 5 ]
print Dumper(@a);
=== push ===
print "\n";
print " push 8";
print "\n";
push(@a, 8); # [ 9 1 2 3 4 5 8 ] <= 8
print Dumper(@a);
=== shift ===
print "\n";
my $shift = shift @a; # 9 <= [ 1 2 3 4 5 8 ]
printf " shift=%d\n", $shift;
print Dumper(@a);
=== pop ===
print "\n";
my $pop = pop(@a); # [ 1 2 3 4 5 ] => 8
printf " pop=%d\n", $pop;
print Dumper(@a);
~~DISCUSSION~~