in reply to P6: grammar: create a hash instead of array?
class H::Actions { # construct a hash from a list of pairs method TOP($/) { make %($<pairs>.ast); } # construct a list of Str => Match pairs method pairs($/){ make @($<pair>».ast) } # construct a Str => Match pair method pair($/) { make ~$<key> => $<mash> }; }
You could also try to stuff all the complexity in method TOP, and use meta operators and parallel dispatch to replace the loop, but I think it will be harder to write, read and debug. Still here's an untested suggestion:
my @a = @($/<pairs><pair>) make %( @a».<key>».Str Z=> @a».<mash> )
since the hash indexing with .<key> is just a method call, it can be applied to all items in an array or list with <c>».</code> parallel dispatch (or >>. if you are an ASCII fan).
Z=> is the Zip meta operator with the => pair constructor. It takes an item from each list, constructs a pair from them, then takes the second item from each list etc.
|
|---|