You'll have to type the $_ of the block as "is copy" if you want to do
this. Another way would be to have "is rw" but that can of course only
work if a container is present in what you map over; there isn't in this
case.
perl6 -e '.perl.say for "hello, how, are, you".split(",").map: -> $_
is copy { s:g/a//; s:g/^ \s|\s $/O/; $_ }'
"hello"
"Ohow"
"Ore"
"Oyou"
But you can make containers exist by assigning to an array:
perl6 -e 'my @data = "hello, how, are, you".split(","); @data.map:
-> $_ is rw { s:g/a//; s:g/^ \s|\s $/O/; $_ }; say @data.perl'
["hello", "Ohow", "Ore", "Oyou"]
Another way to do this a little bit cleaner is with the .subst method:
perl6 -e '.perl.say for "hello, how, are, you".split(",").map: {
.subst(rx/a/, "", :g).subst(rx/^ \s|\s $/, "O", :g) }'
And then you can drop the curly braces and use a Whatever Star to get a
code object out of it:
perl6 -e '.perl.say for "hello, how, are, you".split(",").map:
*.subst(rx/a/, "", :g).subst(rx/^ \s|\s $/, "O", :g)'
Hope that sheds some light!
- Timo
On 09/19/2017 12:38 PM, Luca Ferrari wrote:
> Hi all,
> I'm trying to understand how to use map correctly to apply several
> regexps at once, something like:
>
> my @fields = $line.split( ',' ).map: { s:g/\'//; s:g/^\"|\"$//; $_ };
>
> while the first regexp works, the second fails with "Cannot modify an
> immutable Str", but the topic variable should be considered, not a
> literal string.
> I would replace it with the S operator, but I believe it would not do
> what I want since a new string will be created as output of any
> regexp.
> Any suggestion?
>
> Luca