Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove elements of a Perl array

Is there an easy way to remove n consecutive elements of a Perl array (thus making it shorter in length)?

like image 535
Jean Avatar asked Jul 09 '26 00:07

Jean


2 Answers

You are looking for the Perl builtin function splice, which lets you pick a starting point, number of elements to remove, and an optional replacement list.

my @array = 0 .. 9;

my @slice = splice @array, 3, 3;

say "@slice";   # prints "3 4 5"
say "@array";   # prints "0 1 2 6 7 8 9"
say 0 + @array; # prints 7
like image 143
Eric Strom Avatar answered Jul 10 '26 15:07

Eric Strom


You can use splice to remove array elements.

like image 20
jchips12 Avatar answered Jul 10 '26 13:07

jchips12



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!