Re: match's replacement name?

2014-05-24 Thread perl6-users

On 05/23/2014 09:08 PM, Peter Schwenn wrote:
> Timo, perl6-users,
>
> I don't want to print out the WHOLE text resulting from the match (in
> my case a long file,) but just the /replacement/ string.
>
> [...]
>
> Peter Schwenn
>
> On Thu, May 22, 2014 at 9:58 PM,  > wrote:
>
> [...]
>> perl6-m -e 'my $text = "Well, hello!"; $text ~~ s[ +(\W) ] =
>> my $res = "Rhino$0"; say (:$res); say (:$text)'
>>"res" => "Rhino,"
>> "text" => "Rhino, hello!"
>>
>> Hope to help! - Timo

Peter,

I think you may have missed the trick I put into that piece of code
where I assign to my $res first and chain-assign that into the s[ ... ]
operation. This way, you get only the part that has been replaced (in
the $res variable in this case).

Hope to help
  - Timo


signature.asc
Description: OpenPGP digital signature


Re: match's replacement name?

2014-05-23 Thread Peter Schwenn
Patrick,

I would agree with you that saving all the replacements does NOT warrant
the overhead.  Saving how many matches were made would not be at all heavy
overhead, but again, its easy to do in a closure so that too is not really
necessary.  Just takes awhile to learn how to use the closures.

Thank you,

Peter Schwenn


On Fri, May 23, 2014 at 4:39 PM, Patrick R. Michaud wrote:

> On Fri, May 23, 2014 at 03:08:38PM -0400, Peter Schwenn wrote:
> > Still it would be more straightforward to have something like
> >  $layn ~~ s:g/ (\W) [[RMA\.]? OpenNURBS\.]? I? On (<[2..4]>) dPoint
> > /$0Rhino.Geometry.Point$1d/;
> > and have a more perl6-built-in way of getting hold of the /replacement/
> and
> > the count.
>
> If it's :g, then wouldn't it be "replacement*s*"?
>
> The real question to me is whether this is a common enough use case to
> warrant the overhead of saving the replacement string(s) for every
> s// , or if doing it via closure is sufficient (if a little ugly, but
> IMO it's okay for rare to be a little ugly if it means common remains
> fast(er) :).
>
> Pm
>


Re: match's replacement name?

2014-05-23 Thread Patrick R. Michaud
On Fri, May 23, 2014 at 03:08:38PM -0400, Peter Schwenn wrote:
> Still it would be more straightforward to have something like
>  $layn ~~ s:g/ (\W) [[RMA\.]? OpenNURBS\.]? I? On (<[2..4]>) dPoint
> /$0Rhino.Geometry.Point$1d/;
> and have a more perl6-built-in way of getting hold of the /replacement/ and
> the count.

If it's :g, then wouldn't it be "replacement*s*"?

The real question to me is whether this is a common enough use case to
warrant the overhead of saving the replacement string(s) for every
s// , or if doing it via closure is sufficient (if a little ugly, but
IMO it's okay for rare to be a little ugly if it means common remains
fast(er) :).

Pm


Re: match's replacement name?

2014-05-23 Thread Peter Schwenn
Timo, perl6-users,

I don't want to print out the WHOLE text resulting from the match (in my
case a long file,) but just the /replacement/ string.

I'm sticking with syntax such as (which is also - for me - readable)

$layn ~~ s:g
/ (\W) [[RMA\.]? OpenNURBS\.]? I? On (<[2..4]>) dPoint
/{""if++$n;$r=
"$0Rhino.Geometry.Point$1d"
}/; say "$r $n $/" if $/;

[note that $r only appears once in the rule but both defines and effects
the replacement,  and that{""if++$n;   counts the replacements due to
:g   ].

Still it would be more straightforward to have something like
 $layn ~~ s:g/ (\W) [[RMA\.]? OpenNURBS\.]? I? On (<[2..4]>) dPoint
/$0Rhino.Geometry.Point$1d/;
and have a more perl6-built-in way of getting hold of the /replacement/ and
the count.

Peter Schwenn

On Thu, May 22, 2014 at 9:58 PM,  wrote:

>  [ Sorry for not replying to the whole list earlier, I hope all readers
> will be able to cope with fishing my reply out of the full-quotes. ]
>
> Peter,
>
> Here's what I came up with using the assignment variant of the
> substitution operator:
>
> > perl6-m -e 'my $text = "Well, hello!"; $text ~~ s[ +(\W) ] = my
> $res = "Rhino$0"; say (:$res); say (:$text)'
> > "res" => "Rhino,"
> > "text" => "Rhino, hello!"
>
> Hope to help!
>   - Timo
>
>
>


Re: match's replacement name?

2014-05-22 Thread perl6-users
[ Sorry for not replying to the whole list earlier, I hope all readers
will be able to cope with fishing my reply out of the full-quotes. ]

Peter,

Here's what I came up with using the assignment variant of the
substitution operator:

> perl6-m -e 'my $text = "Well, hello!"; $text ~~ s[ +(\W) ] = my
$res = "Rhino$0"; say (:$res); say (:$text)'
> "res" => "Rhino,"
> "text" => "Rhino, hello!"

Hope to help!
  - Timo

On 05/23/2014 03:46 AM, Peter Schwenn wrote:
> Timo,
>
> Thank you, that works very nicely.  But I'm committed to   s///;  
> instead of .subst
>
> Best I've been able to do is such as:
>
>  $text ~~ s:g/ using \s+ RMA.Rhino (\W) /{$res="using Rhino$0"}$res/;
>  say $res;
>
> which works but makes the s///; quite a bit less readable.
>
> Thank you,
>
> Peter Schwenn
>
> p.s. by the way
>  $res = ($text ~~ s:g/ using \s+ RMA.Rhino (\W) /using Rhino$0/;)
> simply sets $res to True or False as you probably knew
>
>
> On Thu, May 22, 2014 at 8:29 PM, Timo Paulssen  > wrote:
>
>
> On 05/23/2014 01:57 AM, Peter Schwenn wrote:
> > Dear Perl6-users,
> >
> > I'd like to print out the string value of the "replacement" after a
> > match from a statement like:
> >   s/pattern/replacement/; or its .subst version.
> >
> > (I'm able to print out the /pattern/ (match) string simply by
> printing
> > $/ ).
> >
> > Does the /replacement/ have a name so I can print it out too.
> >
> > Thank you,
>
> Hello Peter,
>
> > perl6-m -e 'my $text = "hello world"; my $res =
> $text.subst(/+
> \s+ <( + )>/, "heya!"); say $res.perl;'
> > "hello heya!"
>
> Is this at all what you're looking for?
>
> Cheers,
>   - Timo
>
>



signature.asc
Description: OpenPGP digital signature


Re: match's replacement name?

2014-05-22 Thread Peter Schwenn
Timo,

Thank you, that works very nicely.  But I'm committed to   s///;   instead
of .subst

Best I've been able to do is such as:

 $text ~~ s:g/ using \s+ RMA.Rhino (\W) /{$res="using Rhino$0"}$res/;
 say $res;

which works but makes the s///; quite a bit less readable.

Thank you,

Peter Schwenn

p.s. by the way
 $res = ($text ~~ s:g/ using \s+ RMA.Rhino (\W) /using Rhino$0/;)
simply sets $res to True or False as you probably knew


On Thu, May 22, 2014 at 8:29 PM, Timo Paulssen  wrote:

>
> On 05/23/2014 01:57 AM, Peter Schwenn wrote:
> > Dear Perl6-users,
> >
> > I'd like to print out the string value of the "replacement" after a
> > match from a statement like:
> >   s/pattern/replacement/; or its .subst version.
> >
> > (I'm able to print out the /pattern/ (match) string simply by printing
> > $/ ).
> >
> > Does the /replacement/ have a name so I can print it out too.
> >
> > Thank you,
>
> Hello Peter,
>
> > perl6-m -e 'my $text = "hello world"; my $res = $text.subst(/+
> \s+ <( + )>/, "heya!"); say $res.perl;'
> > "hello heya!"
>
> Is this at all what you're looking for?
>
> Cheers,
>   - Timo
>
>