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 = 'Here be a $(placeholder), for $(purpose) purposes.';

Then, I would probably use simple substitutions, as with this example:

my Str $input = 'Here be a $(placeholder), for $(purpose) purposes.';

sub format-string ($input, %substitutions) {
 my $str = $input;
 for keys %substitutions -> $key {
    $str ~~ s/$key/%substitutions{$key}/;
   }
   return $str;
}
my %substitutes = '$(placeholder)' => "placeholder", '$(purpose)' =>
"testing";
my $output = format-string($input, %substitutes);
say $output;

This is the output running this under the REPL:

> my Str $input = 'Here be a $(placeholder), for $(purpose) purposes.';
Here be a $(placeholder), for $(purpose) purposes.
>
> sub format-string ($input, %substitutions) {
*  my $str = $input;
*  for keys %substitutions -> $key {
*     $str ~~ s/$key/%substitutions{$key}/;
*    }
*    return $str;
* }
sub format-string ($input, %substitutions) { #`(Sub|214745424) ... }
> my %substitutes = '$(placeholder)' => "placeholder", '$(purpose)' =>
"testing";
{$(placeholder) => placeholder, $(purpose) => testing}
> my $output = format-string($input, %substitutes);
Here be a placeholder, for testing purposes.
> say $output;
Here be a placeholder, for testing purposes.

I hope this helps.

Cheers,
Laurent.




Le mar. 28 août 2018 à 12:25, Patrick Spek via perl6-users <
perl6-us...@perl.org> a écrit :

> -----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 grammar here.
>
> So, consider a string, "Here be a $(placeholder), for $(purpose)
> purposes.". I want to be able to put that into a sub, along with some
> Pairs, and get a string with the placeholders replaced back.
>
>     my Str $input = "Here be a $(placeholder), for $(purpose) purposes.";
>     my Str $output = format-string(
>         $input,
>         placeholder => "placeholder",
>         purpose => "testing",
>     );
>
>     dd $output; # "Here be a placeholder, for testing purposes."
>
> The `format-string` sub would call the grammar and apply the actual
> substitution, and that's where I need your help. I am not quite sure
> how I would implement the grammar (and presumably it's actions) to do
> what I want.
>
> Thanks in advance for your help!
>
> - --
> With kind regards,
>
> Patrick Spek
>
>
> www:  https://www.tyil.work/
> mail: p.s...@tyil.nl
> pgp:  EB9E A484 1672 2D37 16F5  A799 9ACF E193 FFBC 1F50
>
> mastodon: @tyil@mastodon.social
> github:   @Tyil
> gitlab:   @tyil
>
> -----BEGIN PGP SIGNATURE-----
>
> iQEzBAEBCAAdFiEE4eL662U9iK2ST2MqN/W6H45XOE8FAluFIw0ACgkQN/W6H45X
> OE+9pAgAry3KwoOS+A5g+y9V0hPHx24nQ6U8TcbZh/HIuvwwbinkCi4oxfhGwTBX
> FXeogYj18OF+K7KEq45fAtB7sqrAelo59elu+ZCXZmiFH1BNHFNWbINkKKWQdHAs
> uYTL7poMwSQ+XQVBTCu3dY32jVl3qasSr4dAM0g6za8TTtgw0TTblF/aNO6A0KC6
> cf757hWxZ7VqprIbpPfvQnB/0BreVu467Va7EGdzZDwi3WfQTg9R0H1NTSkpdS0M
> fyhMJ21rjBhhiUZt+DOHThcd5s7ikk1d1fX5x/hEgRNIcmbQVZ7N3STGSxtqFm6h
> thQ2zhTXGgE/VrTaOHP0a8NiqPamEQ==
> =RlqT
> -----END PGP SIGNATURE-----
>

Reply via email to