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/18 20:04, Laurent Rosenfeld via perl6-users wrote: > 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 <mailto:perl6-us...@perl.org>> a écrit : > > 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! > >