Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 split function adding extra elements to array

Tags:

raku

my @r = split("", "hi");
say @r.elems;
--> output: 4

split is adding two extra elements to the array, one at the beginning and another at the end.

I have to do shift and pop after every split to correct for this.

Is there a better way to split a string?

like image 541
rx57 Avatar asked Sep 04 '17 03:09

rx57


1 Answers

If you're splitting on the empty string, you will get an empty element at the start and the end of the returned list as there is also an empty string before and after the string.

What you want is .comb without parameters, written out completely functionally:

"hi".comb.elems.say;    # 2

See https://docs.raku.org/routine/comb#(Str)_routine_comb for more info.

like image 78
Elizabeth Mattijsen Avatar answered Oct 29 '22 02:10

Elizabeth Mattijsen