Hello All,

I'm trying to understand Raku's split/join rules. Below is REPL code and
output.

I start by studying a 2-element Array in line A (three letters separated by
commas in each element, 6 letters total). In line B, I perform a split/join
and end up with a 1 element result. Since I started with a 2-element Array,
this result suggests that Raku performs a silent join independent of the
join("_") explicitly coded for. In line C, I try looking at split()
independent of join(). Splitting on comma gives me a 5-element result, not
6 elements as I might have expected. Looking at the 5-element result in
line D, I see that "z a" has been joined together into a single element,
head-to-tail.

I further examine these results in lines E and F. In line E, I reverse the
order and do join/split. Again I see a 5-element result, this time with
"z_a" as an element of the result as shown in line F. Because I see a
5-element result in lines C/D without calling join() and a 5 element result
in lines E/F with an explicit call to join(), I conclude that an explicit
call to join() is not necessary for joining together multiple elements of
an array. In other words, split() performs an implied join of Array
elements:

> $*VM
moar (2020.06)
A> my @a = "x,y,z", "a,b,c"; @a.elems.say;
2
B> my @a = "x,y,z", "a,b,c"; @a.split(",").join("_").raku.say;
"x_y_z a_b_c"
C> my @a = "x,y,z", "a,b,c"; @a.split(",").elems.say;
5
D> my @a = "x,y,z", "a,b,c"; @a.split(",").raku.say;
("x", "y", "z a", "b", "c").Seq
E> my @a = "x,y,z", "a,b,c"; @a.join("_").split(",").elems.say;
5
F> my @a = "x,y,z", "a,b,c"; @a.join("_").split(",").raku.say;
("x", "y", "z_a", "b", "c").Seq

So far so good. Now I try looking at some real world data. Below I use the
exact same code on two strings, one with three words and the other with two
words. I assign these strings to an Array as above, then split on
whitespace because these strings are whitespace-delimited and not
comma-delimited as above. What I'm seeing below in lines G/H seems a little
unusual. I would have expected to see a 4 element result with the words
"Valentine Sonic" together in one element. However in line G, I see a 5
element result with each word distinctly separated as shown in line H.

G> my @a = "My Bloody Valentine", "Sonic Youth"; @a.split(" ").elems.say;
5
H> my @a = "My Bloody Valentine", "Sonic Youth"; @a.split(" ").raku.say;
("My", "Bloody", "Valentine", "Sonic", "Youth").Seq

So my question regards "special-casing" of split/join in Raku. Is the first
result on comma-delimited data the default, i.e. joining disparate elements
of an array together head-to-tail? Or is the second result on
whitespace-delimited data the default (i.e. no joining of disparate
elements together head-to-tail)? Which one is special-cased? If the second
one is special-cased, is that why Raku returns 5 elements and not 4
elements as in lines C/D (implied join)?

Finally, when would I ever need/want to have two disparate "terminal"
elements of an Array joined head-to-tail ("z_a") together into a single
element, as in lines A-thru-F?  For example, that means the 2-element array
"'Name1,Password1' 'Name 2,Password2'" becomes the 3-element "'Name1',
Password1 Name 2', Password2'" after a routine split, which seems a little
insecure.

Any insight appreciated,

Best Regards,

Bill.

W. Michels, Ph.D.

Reply via email to