The reason there is a Nil, is you asked for the ord of an empty string.

    "".ord =:= Nil

The reason there are two empty strings is you asked for them.

When you split with "", it will split on every character boundary,
which includes before the first character, and after the last.
That's literally what you asked for.

    my Str $x = "abcd";
    say split( "", $x ).perl;
    # ("", "a", "b", "c", "d", "").Seq

Perl6 doesn't treat this as a special case like other languages do.
You basically asked for this:

    say split( / <after .> | <before .> /, $x ).perl;
    # ("", "a", "b", "c", "d", "").Seq

Perl6 gave you what you asked for.

That is actually useful btw:

    say split( "", "abcd" ).join("|");
    # |a|b|c|d|

You should be using `comb` if you want a list of characters not `split`.

    # these are all identical
    'abcd'.comb.kv
    'abcd'.comb(1).kv
    comb( 1, 'abcd' ).kv

Also why did you add a pointless `@` to `$x` ?
(Actually I'm fairly sure I know why.)

On Tue, Feb 5, 2019 at 11:05 PM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> Hi All,
>
> What is with the starting ending Nils?  There are only four
> elements, why now six?
>
> And how to I correct this?
>
> $ p6 'my Str $x="abcd";
>       for split( "",@$x ).kv -> $i,$j {
>       say "Index <$i> = <$j> = ord <" ~ ord($j) ~ ">";}'
>
> Use of Nil in string context
>    in block  at -e line 1
> Index <0> = <> = ord <>         <----------------- nil ???
> Index <1> = <a> = ord <97>
> Index <2> = <b> = ord <98>
> Index <3> = <c> = ord <99>
> Index <4> = <d> = ord <100>
> Use of Nil in string context
>    in block  at -e line 1
> Index <5> = <> = ord <>         <----------------- nil ???
>
>
> Many thanks,
> -T

Reply via email to