826253 [sage]
>>797 Perl5
公式マニュアルに書かれている通りsplice関数を使ってpush, popを記述できる。
splice(@a,@a,0,$x); # push(@a, $x)
splice(@a,-1); # pop(@a)
なんだけどspliceを使わず言語のstatementだけでpush, pop関数を記述すると…こんな感じ
use v5.18;
use feature 'signatures';
no warnings "experimental::signatures";
sub Push($a, $x) {
@$a = (@$a, $x);
}
sub Pop($a) {
my @a = @$a;
my $b = $a[-1];
@$a = @a[0..$#a-1];
$b;
}
my @s = (1,2,3); # test main
Push(\@s, 4);
say "@s";
my $y = Pop(\@s);
say $y;
実行結果
~ $ perl 18_787_push_pop.pl
1 2 3 4
4
1 2 3
公式マニュアルに書かれている通りsplice関数を使ってpush, popを記述できる。
splice(@a,@a,0,$x); # push(@a, $x)
splice(@a,-1); # pop(@a)
なんだけどspliceを使わず言語のstatementだけでpush, pop関数を記述すると…こんな感じ
use v5.18;
use feature 'signatures';
no warnings "experimental::signatures";
sub Push($a, $x) {
@$a = (@$a, $x);
}
sub Pop($a) {
my @a = @$a;
my $b = $a[-1];
@$a = @a[0..$#a-1];
$b;
}
my @s = (1,2,3); # test main
Push(\@s, 4);
say "@s";
my $y = Pop(\@s);
say $y;
実行結果
~ $ perl 18_787_push_pop.pl
1 2 3 4
4
1 2 3
2020/10/30(金)00:07:47.66(hMjmzAXb.net)