I've searched the Raku Documentation and several books & tutorials and several Stackoverflow posts to learn how to delete an item from a list/array cleanly i.e. without having the (Any) in the place of the deleted element
my @s = <3 18 4 8 92 14 30>;
my $item = 8;
my $index = @s.first($item, :k);
@s[$index]:delete;
This results in [3 18 4 (Any) 92 14 30] and so I can't do any operation on it, e.g. I can't apply [+]
on it.
Is there a way to delete an item from a list/array without that (Any) ?
Yes. Using the splice method:
my @s = <3 18 4 8 92 14 30>;
my $item = 8;
my $index = @s.first($item, :k);
@s.splice($index,1);
say @s; # [3 18 4 92 14 30]
Or you could use the Adverb::Eject module, so you could write the above as:
use Adverb::Eject;
my @s = <3 18 4 8 92 14 30>;
my $item = 8;
my $index = @s.first($item, :k);
@s[$index]:eject;
say @s; # [3 18 4 92 14 30]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With