Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete an element from a list or array without the (Any) artefact in Raku

Tags:

raku

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) ?

like image 436
Lars Malmsteen Avatar asked Feb 25 '20 22:02

Lars Malmsteen


1 Answers

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]
like image 185
Elizabeth Mattijsen Avatar answered Oct 16 '22 18:10

Elizabeth Mattijsen