Use of uninitialized value of type Any in numeric context raku

I don't feel that's a MRE. There are too many issues with it for me to get in to. What I did instead is start from the original Python and translated that. I'll add some comments:

my \s="112213" .comb;      # .comb to simulate Python string[n] indexing.
my \k=2;
my $result=0;              # result is mutated so give it a sigil
for ^s -> \i {             # don't use $^foo vars with for loops 
    my $num_seen = 0;
    my \window = {}
    for i..s-1 -> \ind { 
        if s[ind] == window.keys.none {   # usefully indent code! 
            $num_seen += 1;  
            window{s[ind]} = 1
        } else {
            window{s[ind]} += 1
        }
        if window{s[ind]} == k {
            $num_seen -= 1;
            if $num_seen == 0 {
                $result +=1
            }
        } elsif window{s[ind]} > k {
            last
        }
    }
}
print($result)

displays 4.

I'm not saying that's a good solution in Raku. It's just a relatively mechanical translation. Hopefully it's helpful.


As usual, the answer by @raiph is correct. I just want to do the minimal changes to your program that get it right. In this case, it's simply adding indices to both loops to make stuff clearer. You were using the context variable $_ in the first, and $^a in the second (inner), and it was getting unnecesarily confusing.

my @s=<1 1 2 2 1 3>;
my $k=2;
my $res=0;
for ^@s -> $i {
    my $seen = 0;
    my %window;
    for @s[$i..*] -> $c {
    if $c == %window.keys.none {
            $seen++;
            %window{$c} = 1;
    } else {  
            %window{$c} += 1;
    }
    if %window{$c} == $k {
            $seen--;
            if $seen == 0 {
        $res++;
        }
    } elsif %window{$c} > $k {
            last;
    }
    }
}
say $res;

As you see , besides trying to indent everything a bit more properly, the only additional thing is to add -> $i and -> $c so that loops are indexed, and then use them where you were using implicit variables.

Tags:

Raku