How to know missing values ​​during multi-valued iteration in Perl 6?

During a multi-valued iteration, if we run out of values, the last group of values ​​will not be processed in the current version of Rakudo.

my @arr = 1, 2, 3, 4, 5, 6
for @arr -> $a, $b, $c, $d {
  say $a
  say $b
  say $c
  say $d
}

Result

1
2
3
4

which falls 5and 6.

So how can I get deleted items?

+6
source share
2 answers

As @choroba answers, your code really throws an error in Perl 6.c.

, 5, 6, $a, $b, $c, $d, .

:

A) .

for @arr -> $a, $b?, $c?, $d? {
    say "---";
    say $a;
    say $b if $b.defined;
    say $c if $c.defined;
    say $d if $d.defined;
}

? – , Mu ( "most undefined" ). , , , for .
:

---
1
2
3
4
---
5
6

B) .

for @arr -> $a, $b = 'default-B', $c = 'default-C', $d = 'default-D' {
    say ($a, $b, $c, $d);
}

, @choroba. , , , Mu , .
:

(1 2 3 4)
(5 6 default-C default-D)

C) .rotor .

for - n-at-time. .rotor . , :partial :

for @arr.rotor(4, :partial) -> @slice {
    say @slice;
}

@slice 4 , , . ( @slice.elems , , .) < > :

(1 2 3 4)
(5 6)
+7

Rakudo Star :

===SORRY!=== Error while compiling /home/choroba/1.p6
Unexpected block in infix position (missing statement control word before the expression?)
at /home/choroba/1.p6:2
------> for @arr⏏ -> $a, $b, $c, $d {
    expecting any of:
        infix
        infix stopper

1
2
3
4
Too few positionals passed; expected 4 arguments but got 2
  in block <unit> at /home/choroba/1.p6 line 2

, :

for @arr -> $a,
            $b = 'Missing b',
            $c = 'Missing c',
            $d = 'Missing d' {

smls , :

for @arr -> $a, $b?, $c?, $d? {

, $var = Mu.

+5

All Articles