Nested templated data: Difference between revisions

Content added Content deleted
(added Factor)
(→‎{{header|Perl 6}}: More concisely)
Line 89: Line 89:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{works with|Rakudo|2018.04.01}}
{{works with|Rakudo|2018.04.01}}
Explicitly not using strings, using one data structure to fill in another. Since it ''isn't'' a string, the output format removes the newlines from the template; line feeds aren't particularly significant in Perl 6 data structures. It does preserve the nesting though. In the second example, payload "buckets" that don't exist result in an undefined value being inserted; by default: Any.
Explicitly not using strings, using one data structure to fill in another. Since it ''isn't'' a string, the output format removes the newlines from the template; line feed (white space in general) isn't particularly significant in Perl 6 data structures. It does preserve the nesting though. In the second example, payload "buckets" that don't exist result in an undefined value being inserted; by default: Any.
<lang perl6>my @payloads = <
<lang perl6>say join "\n ", '##PAYLOADS:', |my @payloads = 'Payload#' X~ ^7;
Payload#0
Payload#1
Payload#2
Payload#3
Payload#4
Payload#5
Payload#6
>;

sub template ($pattern) { @payloads[|$pattern] };


for [
for [
Line 111: Line 101:
5),)
5),)
] {
] {
say ' Template: ', $_.perl;
say "\n Template: ", $_.perl;
say "Data structure: { template($_).perl }\n";
say "Data structure: { @payloads[|$_].perl }";
}</lang>
}</lang>
{{out}}
{{out}}
<pre>##PAYLOADS:
<pre> Template: $(((1, 2), (3, 4, 1), 5),)
Payload#0
Payload#1
Payload#2
Payload#3
Payload#4
Payload#5
Payload#6

Template: $(((1, 2), (3, 4, 1), 5),)
Data structure: ((("Payload#1", "Payload#2"), ("Payload#3", "Payload#4", "Payload#1"), "Payload#5"),)
Data structure: ((("Payload#1", "Payload#2"), ("Payload#3", "Payload#4", "Payload#1"), "Payload#5"),)