Here's what I ended up with:
my @tracks = <Foo Ba(r B^az>;
sub comments {
my @filteredtitles;
for @tracks -> $_ is copy {
s:g / <[\(^]> //;
@filteredtitles.push: $_;
}
return @filteredtitles;
}
The is copy
ensures the variable set up by the for
loop is mutable.
The s:g/...//;
is all that's needed to strip the unwanted characters.
One thing no one can help you with is the error you reported. I currently think you just got confused.
Here's an example of code that generates that error:
do { @_ }
But there is no way the code you've shared could generate that error because it requires that there is an @_
variable in your code, and there isn't one.
One way I can help in relation to future problems you may report on StackOverflow is to encourage you to read and apply the guidance in Minimal Reproducible Example.
While your code did not generate the error you reported, it will perhaps help you if you know about some of the other compile time and run time errors there were in the code you shared.
Compile-time errors:
There were several run-time errors, once I got past the compile-time errors. I'll discuss just one, the regex:
.*<?[...]>
will match any sub-string with a final character that's one of the ones listed in the [...]
, and will then capture that sub-string except without the final character. In the context of an s:g/...//
substitution this will strip ordinary characters (captured by the .*
) but leave the special characters.
This makes no sense.
So I dropped the .*
, and also the ?
from the special character pattern, changing it from <?[...]>
(which just tries to match against the character, but does not capture it if it succeeds) to just <[...]>
(which also tries to match against the character, but, if it succeeds, does capture it as well).
A final comment is about an error you made that may well have seriously confused you.
In a nutshell, the s///
construct must have three slashes.
In your question you had code of the form s/.../
(or s:g/.../
etc), without the final slash. If you try to compile such code the parser gets utterly confused because it will think you're just writing a long replacement string.
For example, if you wrote this code:
if s/foo/ { say 'foo' }
if m/bar/ { say 'bar' }
it'd be as if you'd written:
if s/foo/ { say 'foo' }\nif m/...
which in turn would mean you'd get the compile-time error:
Missing block
------> if m/⏏bar/ { ... }
expecting any of:
block or pointy block
...
because Raku(do) would have interpreted the part between the second and third /
s as the replacement double quoted string of what it interpreted as an s/.../.../
construct, leading it to barf when it encountered bar
.
So, to recap, the s///
construct requires three slashes, not two.
(I'm ignoring syntactic variants of the construct such as, say, s [...] = '...'
.)