Let's use "trans" instead of a regular expression, updating my example:
sub format-string(Str:D $source, *%vars --> Str:D) {
return $source.trans(
[%vars.keys.map: '$(' ~ * ~ ')' ] => [%vars.values]
);
}
so simple! But, it does not catch missing variables... I bet we can fix
th
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256
It's supposed to be simple, since I don't need much. The most basic of
substitution is all I care about right now. I've implemented it with a
simple regex now, and it seems to work just fine. It's available as a
module[1] now, and it's used in anothe
I should point out that the trans method has a mode that lets you pass
placeholders and substitutions and it will Do The Right Thing regarding
overlaps and order and everything.
On 29/08/18 16:21, Timo Paulssen wrote:
> There's a problem with your code, if any of the substitutions contains
> some
Surely, Timo, it might happen with some data, but not with the type of data
shown by the OP (where the words to be replaced were all in the form
$(something) and not the replacements. In fact, I suspect that many other
things might go wrong with spurious data.
The point is that my suggestion was j
There's a problem with your code, if any of the substitutions contains
something that looks like the placeholder thing, and if it comes later
in the iteration of the hash keys (which is randomized now) it will
substitute again. This is very likely not what you want, though.
HTH
- Timo
On 28/08/
Oops, forgot to hit "reply-all" this morning... similar answer to
Laurent's with slightly different implementation.
-y
-- Forwarded message -
From: yary
Date: Tue, Aug 28, 2018 at 8:43 AM
Subject: Re: A grammar to provide substitution
this is simple enough t
Hi Patrick,
for note that this codeline:
my Str $input = "Here be a $(placeholder), for $(purpose) purposes.";
will not compile because Perl will try to interpolate $(placeholder) and
$(purpose) as vairables that have not been declared.
You need to use non interpolating quotes:
my Str $input =
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA256
Hi all,
I'm trying to substitute parts of a string, and thought this might be a
good use of a grammar. Sadly, grammars aren't my strong suit, so I
thought I'd ask the wider community for help. Maybe you guys know an
even better solution than using a