replace s///r ?

hello people,

in perl5, i can

        print for
            map  s/^/4/r,
            grep /^\d+$/,
            <ARGV>

the perl6 version is a Seq, so much more memory friendly
but i expected (and haven't found in the documentation)
a short equivalent of s///r so the shorter i have is:

    $*ARGFILES
     . lines
     . grep( / ^ <digit>+ $ / )
     .  map( *.subst(/^/," * ") )
     .  map(&other-things-to-do)
     .  map(&say)

when, of course, i want to replace

     .  map( *.subst(/^/," * ") )

by something like

     .  map( s:r/^/* / )

any idea?

regards
marc
0
marc
6/8/2019 5:29:23 PM
perl.perl6.users 1217 articles. 0 followers. Follow

6 Replies
63 Views

Similar Articles

[PageSpeed] 14

p6 can transliterate from p5 rather literally

say .subst(/^/,4) for grep /^\d+$/, (325, '44a', 555, 6);



-y

-y


On Mon, Jun 10, 2019 at 9:19 PM Marc Chantreux
<marc.chantreux@renater.fr> wrote:
>
> hello people,
>
> in perl5, i can
>
>         print for
>             map  s/^/4/r,
>             grep /^\d+$/,
>             <ARGV>
>
> the perl6 version is a Seq, so much more memory friendly
> but i expected (and haven't found in the documentation)
> a short equivalent of s///r so the shorter i have is:
>
>     $*ARGFILES
>      . lines
>      . grep( / ^ <digit>+ $ / )
>      .  map( *.subst(/^/," * ") )
>      .  map(&other-things-to-do)
>      .  map(&say)
>
> when, of course, i want to replace
>
>      .  map( *.subst(/^/," * ") )
>
> by something like
>
>      .  map( s:r/^/* / )
>
> any idea?
>
> regards
> marc
0
not
6/11/2019 5:54:21 AM
Or if you need to "map" the substitution

..say for (325, '44a', 555, 6).grep(/^\d+$/).map( *.subst(/^/,4) )


-y

On Mon, Jun 10, 2019 at 10:54 PM yary <not.com@gmail.com> wrote:
>
> p6 can transliterate from p5 rather literally
>
> say .subst(/^/,4) for grep /^\d+$/, (325, '44a', 555, 6);
>
>
>
> -y
>
> -y
>
>
> On Mon, Jun 10, 2019 at 9:19 PM Marc Chantreux
> <marc.chantreux@renater.fr> wrote:
> >
> > hello people,
> >
> > in perl5, i can
> >
> >         print for
> >             map  s/^/4/r,
> >             grep /^\d+$/,
> >             <ARGV>
> >
> > the perl6 version is a Seq, so much more memory friendly
> > but i expected (and haven't found in the documentation)
> > a short equivalent of s///r so the shorter i have is:
> >
> >     $*ARGFILES
> >      . lines
> >      . grep( / ^ <digit>+ $ / )
> >      .  map( *.subst(/^/," * ") )
> >      .  map(&other-things-to-do)
> >      .  map(&say)
> >
> > when, of course, i want to replace
> >
> >      .  map( *.subst(/^/," * ") )
> >
> > by something like
> >
> >      .  map( s:r/^/* / )
> >
> > any idea?
> >
> > regards
> > marc
0
not
6/11/2019 6:01:51 AM
Though, it's an odd use of s///r in p5 and subst in p6, when all it is
doing is prepending a string!

 .say for (325, '44a', 555, 6).grep(/^\d+$/).map( '4' ~ * )


-y

On Mon, Jun 10, 2019 at 11:01 PM yary <not.com@gmail.com> wrote:
>
> Or if you need to "map" the substitution
>
> .say for (325, '44a', 555, 6).grep(/^\d+$/).map( *.subst(/^/,4) )
>
>
> -y
>
> On Mon, Jun 10, 2019 at 10:54 PM yary <not.com@gmail.com> wrote:
> >
> > p6 can transliterate from p5 rather literally
> >
> > say .subst(/^/,4) for grep /^\d+$/, (325, '44a', 555, 6);
> >
> >
> >
> > -y
> >
> > -y
> >
> >
> > On Mon, Jun 10, 2019 at 9:19 PM Marc Chantreux
> > <marc.chantreux@renater.fr> wrote:
> > >
> > > hello people,
> > >
> > > in perl5, i can
> > >
> > >         print for
> > >             map  s/^/4/r,
> > >             grep /^\d+$/,
> > >             <ARGV>
> > >
> > > the perl6 version is a Seq, so much more memory friendly
> > > but i expected (and haven't found in the documentation)
> > > a short equivalent of s///r so the shorter i have is:
> > >
> > >     $*ARGFILES
> > >      . lines
> > >      . grep( / ^ <digit>+ $ / )
> > >      .  map( *.subst(/^/," * ") )
> > >      .  map(&other-things-to-do)
> > >      .  map(&say)
> > >
> > > when, of course, i want to replace
> > >
> > >      .  map( *.subst(/^/," * ") )
> > >
> > > by something like
> > >
> > >      .  map( s:r/^/* / )
> > >
> > > any idea?
> > >
> > > regards
> > > marc
0
not
6/11/2019 6:06:28 AM
The /r modifier on s/// in Perl5 is S/// in Perl6.

Though you have to put it into a block lambda, otherwise it applies to
$_ before it gets a chance to be passed to map.

    $*ARGFILES
     . lines
     . grep( / ^ <digit>+ $ / )
     .  map( {S/^/ * /} )
     .  map(&other-things-to-do)
     .  map(&say)

On Mon, Jun 10, 2019 at 11:19 PM Marc Chantreux
<marc.chantreux@renater.fr> wrote:
>
> hello people,
>
> in perl5, i can
>
>         print for
>             map  s/^/4/r,
>             grep /^\d+$/,
>             <ARGV>
>
> the perl6 version is a Seq, so much more memory friendly
> but i expected (and haven't found in the documentation)
> a short equivalent of s///r so the shorter i have is:
>
>     $*ARGFILES
>      . lines
>      . grep( / ^ <digit>+ $ / )
>      .  map( *.subst(/^/," * ") )
>      .  map(&other-things-to-do)
>      .  map(&say)
>
> when, of course, i want to replace
>
>      .  map( *.subst(/^/," * ") )
>
> by something like
>
>      .  map( s:r/^/* / )
>
> any idea?
>
> regards
> marc
0
b2gills
6/11/2019 1:26:11 PM
hello,

On Tue, Jun 11, 2019 at 08:26:11AM -0500, Brad Gilbert wrote:
> The /r modifier on s/// in Perl5 is S/// in Perl6.

oops .. it was the section just after s/// :( thanks for pointing out.

> Though you have to put it into a block lambda, otherwise it applies to
> $_ before it gets a chance to be passed to map.

this is very kind to let perl5 win sometimes :)

regards
marc
0
eiro
6/11/2019 2:40:04 PM
Thank you Yary. It's not often I have to prepend a "4" to a mixed list
items of containing numbers, but your code is perfect for extracting
the numbers and prepending a dollar sign ($):

>  .say for (325, '44a', 555, 6).grep(/^\d+$/).map( '$' ~ * )
$325
$555
$6

Best Regards,

Bill.



On Mon, Jun 10, 2019 at 11:02 PM yary <not.com@gmail.com> wrote:
>
> Or if you need to "map" the substitution
>
> .say for (325, '44a', 555, 6).grep(/^\d+$/).map( *.subst(/^/,4) )
>
>
> -y
>
> On Mon, Jun 10, 2019 at 10:54 PM yary <not.com@gmail.com> wrote:
> >
> > p6 can transliterate from p5 rather literally
> >
> > say .subst(/^/,4) for grep /^\d+$/, (325, '44a', 555, 6);
> >
> >
> >
> > -y
> >
> > -y
> >
> >
> > On Mon, Jun 10, 2019 at 9:19 PM Marc Chantreux
> > <marc.chantreux@renater.fr> wrote:
> > >
> > > hello people,
> > >
> > > in perl5, i can
> > >
> > >         print for
> > >             map  s/^/4/r,
> > >             grep /^\d+$/,
> > >             <ARGV>
> > >
> > > the perl6 version is a Seq, so much more memory friendly
> > > but i expected (and haven't found in the documentation)
> > > a short equivalent of s///r so the shorter i have is:
> > >
> > >     $*ARGFILES
> > >      . lines
> > >      . grep( / ^ <digit>+ $ / )
> > >      .  map( *.subst(/^/," * ") )
> > >      .  map(&other-things-to-do)
> > >      .  map(&say)
> > >
> > > when, of course, i want to replace
> > >
> > >      .  map( *.subst(/^/," * ") )
> > >
> > > by something like
> > >
> > >      .  map( s:r/^/* / )
> > >
> > > any idea?
> > >
> > > regards
> > > marc
0
perl6
6/12/2019 8:16:35 PM
Reply: