Nested templated data: Difference between revisions

add SETL
(add SETL)
 
(8 intermediate revisions by 7 users not shown)
Line 13:
;Task Detail:
Given the following input template <code>t</code> and list of payloads <code>p</code>:
<syntaxhighlight lang="text"># Square brackets are used here to denote nesting but may be changed for other,
# clear, visual representations of nested data appropriate to ones programming
# language.
Line 21:
5]]
 
p = 'Payload#0' ... 'Payload#6'</langsyntaxhighlight>
 
The correct output should have the following structure, (although spacing and
linefeeds may differ, the nesting and order should follow):
<syntaxhighlight lang="text">[[['Payload#1', 'Payload#2'],
['Payload#3', 'Payload#4', 'Payload#1'],
'Payload#5']]</langsyntaxhighlight>
 
1. Generate the output for the above template, <code>t</code>.<br>
Line 38:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">;-----------------------------------------------------------
Nested_templated_data(template, Payload){
UsedP := [], UnusedP := [], UnusedI := []
Line 82:
return nobj
}
;-----------------------------------------------------------</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">T := [[[1,2] , [3,4,1] , 5]]
; Autohotkey uses 1-based objects/arrays
P := ["Payload#1", "Payload#2", "Payload#3", "Payload#4", "Payload#5", "Payload#6", "Payload#7"]
Line 89:
output := Results.1
Undefined_indices := Results[2]
Unused_Payloads := Results[3]</langsyntaxhighlight>
{{out}}
<pre>T := [[[1,2] , [3,4,1] , 5]]
Line 102:
Undefined_indices : [8]
Unused_Payloads : ["Payload#6", "Payload#7"]</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">dim p(7)
dim p$(7)
p$[0] = "Payload#0" : p$[1] = "Payload#1"
p$[2] = "Payload#2" : p$[3] = "Payload#3"
p$[4] = "Payload#4" : p$[5] = "Payload#5"
p$[6] = "Payload#6"
dim q(7) fill false
dim t(4, 5)
t[0, 0] = 1: t[0, 1] = 2
t[1, 0] = 3: t[1, 1] = 4: t[1, 2] = 1
t[2, 0] = 5
 
for i = 0 to t[?][]-1
for j = 0 to t[?][]-1
if t[i, j] <> 0 then
q[t[i, j]] = true
t[i, j] += 1
end if
next j
next i
 
for i = 0 to t[?][]-1
for j = 0 to t[?][]-1
if t[i, j] <> 0 then print p$[t[i, j] -1]; " ";
next j
print
next i
 
for i = 0 to q[?]-1
if not q[i] then print p$[i]; " is not used"
next i</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="vbnet">100 cls
110 dim i,j,p(6)
120 dim p$(6)
130 p$(0) = "Payload#0" : p$(1) = "Payload#1"
140 p$(2) = "Payload#2" : p$(3) = "Payload#3"
150 p$(4) = "Payload#4" : p$(5) = "Payload#5"
160 p$(6) = "Payload#6"
170 dim q(6)
180 dim t(2,3)
190 t(0,0) = 1 : t(0,1) = 2
200 t(1,0) = 3 : t(1,1) = 4 : t(1,2) = 1
210 t(2,0) = 5
220 for i = 0 to ubound(t)
230 for j = 0 to ubound(t,2)
240 if t(i,j) <> 0 then
250 q(t(i,j)) = true
260 t(i,j) = t(i,j)+1
270 endif
280 next j
290 next i
300 for i = 0 to ubound(t)
310 for j = 0 to ubound(t,2)
320 if t(i,j) <> 0 then print p$(t(i,j)-1);" ";
330 next j
340 print
350 next i
360 for i = 0 to ubound(q)
370 if not q(i) then print p$(i);" is not used"
380 next i
390 end</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
{{trans|Ring}}
<syntaxhighlight lang="vbnet">Dim As Integer t(2, 3) = {{1,2},{3,4,1},{5}}
Dim As Integer i, j, p(6)
Dim As String pStr(6) = {"Payload#0", "Payload#1", "Payload#2", "Payload#3", "Payload#4", "Payload#5", "Payload#6"}
Dim As Boolean q(6)
 
For i = Lbound(t) To Ubound(t)
For j = Lbound(t, 2) To Ubound(t, 2)
If t(i, j) <> 0 Then
q(t(i, j)) = True
t(i, j) += 1
End If
Next j
Next i
 
For i = Lbound(t) To Ubound(t)
For j = Lbound(t, 2) To Ubound(t, 2)
If t(i, j) <> 0 Then Print pStr(t(i, j)-1); " ";
Next j
Print
Next i
 
For i = Lbound(q) To Ubound(q)
If q(i) = False Then Print pStr(i); " is not used"
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>Payload#1 Payload#2
Payload#3 Payload#4 Payload#1
Payload#5
Payload#0 is not used
Payload#6 is not used</pre>
 
==={{header|GW-BASIC}}===
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Applesoft BASIC}}
{{works with|Chipmunk Basic}}
{{works with|MSX BASIC}}
{{works with|QBasic}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">110 TRUE = -1
120 FALSE = NOT TRUE
130 DIM I,J,P(6)
140 DIM P$(6)
150 P$(0) = "Payload#0" : P$(1) = "Payload#1"
160 P$(2) = "Payload#2" : P$(3) = "Payload#3"
170 P$(4) = "Payload#4" : P$(5) = "Payload#5"
180 P$(6) = "Payload#6"
190 DIM Q(6)
200 DIM T(2,3)
210 T(0,0) = 1 : T(0,1) = 2
220 T(1,0) = 3 : T(1,1) = 4 : T(1,2) = 1
230 T(2,0) = 5
240 FOR I = 0 TO 2
250 FOR J = 0 TO 3
260 IF T(I,J) <> 0 THEN Q(T(I,J)) = TRUE : T(I,J) = T(I,J)+1
270 NEXT J
280 NEXT I
290 FOR I = 0 TO 2
300 FOR J = 0 TO 3
310 IF T(I,J) <> 0 THEN PRINT P$(T(I,J)-1);" ";
320 NEXT J
330 PRINT
340 NEXT I
350 FOR I = 0 TO 6
360 IF Q(I) = FALSE THEN PRINT P$(I);" is not used"
370 NEXT I
380 END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">True = -1: False = NOT True
 
DIM p(6)
DIM p$(6)
p$(0) = "Payload#0": p$(1) = "Payload#1"
p$(2) = "Payload#2": p$(3) = "Payload#3"
p$(4) = "Payload#4": p$(5) = "Payload#5"
p$(6) = "Payload#6"
DIM q(6)
DIM t(2, 3)
t(0, 0) = 1: t(0, 1) = 2
t(1, 0) = 3: t(1, 1) = 4: t(1, 2) = 1
t(2, 0) = 5
 
FOR i = LBOUND(t) TO UBOUND(t) '0 To 2
FOR j = LBOUND(t, 2) TO UBOUND(t, 2) '0 To 3
IF t(i, j) <> 0 THEN
q(t(i, j)) = True
t(i, j) = t(i, j) + 1
END IF
NEXT j
NEXT i
 
FOR i = LBOUND(t) TO UBOUND(t) '0 To 2
FOR j = LBOUND(t, 2) TO UBOUND(t, 2) '0 To 3
IF t(i, j) <> 0 THEN PRINT p$(t(i, j) - 1); " ";
NEXT j
PRINT
NEXT i
 
FOR i = LBOUND(q) TO UBOUND(q)
IF q(i) = False THEN PRINT p$(i); " is not used"
NEXT i</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|QB64}}===
The [[#QBasic|QBasic]] solution works without any changes.
 
==={{header|Run BASIC}}===
{{trans|QBasic}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|QBasic}}
{{works with|QB64}}
<syntaxhighlight lang="qbasic">DIM p(6)
DIM p$(6)
p$(0) = "Payload#0": p$(1) = "Payload#1"
p$(2) = "Payload#2": p$(3) = "Payload#3"
p$(4) = "Payload#4": p$(5) = "Payload#5"
p$(6) = "Payload#6"
DIM q(6)
DIM t(2, 3)
t(0, 0) = 1: t(0, 1) = 2
t(1, 0) = 3: t(1, 1) = 4: t(1, 2) = 1
t(2, 0) = 5
 
FOR i = 0 TO 2
FOR j = 0 TO 3
IF t(i, j) <> 0 THEN
q(t(i, j)) = -1
t(i, j) = t(i, j) + 1
END IF
NEXT j
NEXT i
 
FOR i = 0 TO 2
FOR j = 0 TO 3
IF t(i, j) <> 0 THEN PRINT p$(t(i, j) - 1); " ";
NEXT j
PRINT
NEXT i
 
FOR i = 0 TO 6
IF q(i) = 0 THEN PRINT p$(i); " is not used"
NEXT i</syntaxhighlight>
{{out}}
<pre>Same as QBasic entry.</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">dim p(6)
dim p$(6)
p$(0) = "Payload#0" : p$(1) = "Payload#1"
p$(2) = "Payload#2" : p$(3) = "Payload#3"
p$(4) = "Payload#4" : p$(5) = "Payload#5"
p$(6) = "Payload#6"
dim q(6)
dim t(2, 3)
t(0, 0) = 1: t(0, 1) = 2
t(1, 0) = 3: t(1, 1) = 4: t(1, 2) = 1
t(2, 0) = 5
 
for i = 0 to arraysize(t(), 1)
for j = 0 to arraysize(t(), 2)
if t(i, j) <> 0 then
q(t(i, j)) = True
t(i, j) = t(i, j) + 1
fi
next j
next i
 
for i = 0 to arraysize(t(), 1)
for j = 0 to arraysize(t(), 2)
if t(i, j) <> 0 print p$(t(i, j) -1), " ";
next j
print
next i
 
for i = 0 to arraysize(q(), 1)
if not q(i) print p$(i), " is not used"
next i</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|Bracmat}}==
The uninstantiated template and the instantiated template are JSON structures. The payloads are listed in a Bracmat list. The <code>get</code> function is instructed to read input from memory and to parse it as JSON. The output of this call is a Bracmat structure (not shown) that is assigned to <code>template</code>. The <code>instantiate</code> function recursively traverses the template's tree structure. If the current node is a number, then that number is used as the key into the payloads list. The corresponding payload is then returned. If the current node is not a number, then it is assumed that the current node is a binary (sub)tree. The left hand side (called <code>a</code>) and the right hand side (called <code>b</code>) are instantiated and their combination is returned. Finally the instantiated tree is transformed back to JSON format and output.
<langsyntaxhighlight lang="bracmat">( get$("[
[[1, 2],
[3, 4, 1],
Line 129 ⟶ 403:
)
& out$(jsn$(instantiate$(!template.!payloads)))
);</langsyntaxhighlight>
{{out}}
<pre>[[[payload#1,payload#2],[payload#3,payload#4,payload#1],payload#5]]</pre>
Line 138 ⟶ 412:
The nested indexes are defined by tuples from the standard library. C++ function templates recursively traverse the tuple structure and map to the payloads.
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <set>
#include <tuple>
Line 224 ⟶ 498:
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 246 ⟶ 520:
 
=={{header|Crystal}}==
<langsyntaxhighlight Rubylang="ruby">def with_payload(template, payload, used = nil)
template.map do |item|
if item.is_a? Enumerable
Line 263 ⟶ 537:
 
unused = Set(Int32).new((0..6).to_a) - used
puts "Unused indices: #{unused}"</langsyntaxhighlight>
 
{{out}}
Line 271 ⟶ 545:
=={{header|Factor}}==
Words for traversing nested sequences can be found in the <code>sequences.deep</code> vocabulary. Factor's prettyprinter attempts to print structures on a single line (64 characters by default, though this can be changed) if they will fit. Otherwise, the prettyprinter will break them up into multiple lines, preferring to show one member per line if possible. <code>f</code>, Factor's false/nil value, is used to indicate a missing payload.
<langsyntaxhighlight lang="factor">USING: formatting kernel literals math sequences sequences.deep ;
IN: rosetta-code.nested-template-data
 
Line 288 ⟶ 562:
[ dup insert-payloads "Template: %u\nData Structure: %u\n"
printf ] bi@</langsyntaxhighlight>
{{out}}
<pre>
Line 312 ⟶ 586:
Go's standard library includes a "text/template" package which can be used for this task.
<br>
The integer indices are represented by the keys of a map whose corresponding value is the appropriate payload string. Templates have their own mini-language and, for a map P with key n, the expression: <syntaxhighlight lang ="html">{{index .P n}}</langsyntaxhighlight> will be replaced by the corresponding payload.
<br>
If an integer index either doesn't exist in the map or maps to an empty payload, then the above expression will simply be replaced by an empty string when the template is executed.
<langsyntaxhighlight lang="go">package main
 
import (
Line 350 ⟶ 624:
sort.Ints(unused)
fmt.Println("\nThe unused payloads have indices of :", unused)
}</langsyntaxhighlight>
 
{{out}}
Line 372 ⟶ 646:
If we do not bother about neat textual representation, the definition of traversable nested template is as easy as following:
 
<langsyntaxhighlight lang="haskell">{-# language DeriveTraversable #-}
 
data Template a = Val a | List [Template a]
Line 378 ⟶ 652:
, Functor
, Foldable
, Traversable )</langsyntaxhighlight>
 
Functorial property of a data type is sufficient for defining the universal substitutor of keys/indices by payload data:
<langsyntaxhighlight lang="haskell">subst :: (Functor t, Eq i) => [(i,a)] -> t i -> t (Maybe a)
subst d = fmap (`lookup` d)</langsyntaxhighlight>
 
It is possible to turn any list of payloads into a map by simple helper function:
 
<langsyntaxhighlight lang="haskell">indexed :: [a] -> [(Int, a)]
indexed = zip [0..]</langsyntaxhighlight>
 
Now we can make substitutions to flat lists or nested templates:
 
<langsyntaxhighlight lang="haskell">t :: Template Int
t = List [ List [Val 1, Val 2]
, List [Val 3, Val 4, Val 10]
Line 397 ⟶ 671:
 
payloads :: [(Int, String)]
payloads = [(i, "payload#"++show i) | i <- [0..6]]</langsyntaxhighlight>
 
<pre>λ> subst payloads [6,2,1,2]
Line 426 ⟶ 700:
Foldable properties of templates (with a little help of monoids) allow to implement extended tasks.
 
<langsyntaxhighlight lang="haskell">unusedIndices :: (Foldable t, Eq i) => [(i,a)] -> t i -> [i]
unusedIndices d = foldMap unused
where unused i = maybe (pure i) (pure []) $ lookup i d
Line 432 ⟶ 706:
unusedData :: (Foldable t, Eq i) => [(i, a)] -> t i -> [(i,a)]
unusedData = foldr delete
where delete i = filter ((i /= ) . fst) </langsyntaxhighlight>
 
<pre>λ> unusedIndices payloads t
Line 642 ⟶ 916:
 
For the main task, the key ideas here are (1) to use a JSON dictionary to store the integer-to-payload mapping; and (2) to use the built-in filter, `walk/1`, to instantiate the template.
<syntaxhighlight lang="sh">
<lang sh>
#!/bin/bash
function template {
Line 665 ⟶ 939:
| walk(if type == "number" then $dict[tostring] else . end)
'
</syntaxhighlight>
</lang>
This produces the expected output, as also shown below.
 
For the second task, we could separately invoke jq similarly:
<syntaxhighlight lang="sh">
<lang sh>
payload | jq -Rn --argfile t <(template) '
([inputs] | with_entries(.key |= tostring)) as $dict
| $dict[(($dict|keys_unsorted) - ([ $t | .. | numbers ] | unique | map(tostring) ))[]]
'
</syntaxhighlight>
</lang>
 
Or, if you prefer one invocation of jq for both tasks:<langsyntaxhighlight lang="sh">
payload | jq -Rrn --argfile t <(template) '
([inputs] | with_entries(.key |= tostring)) as $dict
Line 683 ⟶ 957:
"\nUnused payload",
$dict[(($dict|keys_unsorted) - ([ $t | .. | numbers ] | unique | map(tostring) ))[]]
'</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">[
[
[
Line 702 ⟶ 976:
Unused payload
Payload#0
Payload#6</langsyntaxhighlight>
 
=={{header|Julia}}==
The array structure needs to be specified as Any type of data, to allow assignment of String to positions in the array originally containing integers.
<langsyntaxhighlight lang="julia">t = ([Any[Any[1, 2],
Any[3, 4, 1],
5]])
Line 732 ⟶ 1,006:
 
show(t)
</langsyntaxhighlight>{{output}}<pre>
Array{Any,1}[[Any["Payload#1", "Payload#2"], Any["Payload#3", "Payload#4", "Payload#1"], "Payload#5"]]
</pre>
 
=={{header|M2000Mathematica}}/{{header|Wolfram InterpreterLanguage}}==
<syntaxhighlight lang="mathematica">t = ToExpression[StringReplace["[[[1,2],[3,4,1],5]]", {"[" -> "{", "]" -> "}"}]];
p = "Payload#" <> ToString[#] & /@ Range[6];
Map[p[[#]] &, t, {-1}]</syntaxhighlight>
{{out}}
<pre>{{{"Payload#1", "Payload#2"}, {"Payload#3", "Payload#4", "Payload#1"}, "Payload#5"}}</pre>
 
 
<lang M2000 Interpreter>
=={{header|M2000 Interpreter}}==
Font "Courier New"
<syntaxhighlight lang="m2000 interpreter">Font "Courier New"
cls
Module Checkit {
Line 885 ⟶ 1,165:
}
Checkit
Report clipboard$</syntaxhighlight>
</lang>
{{out}}
<pre style="height:30ex;overflow:scroll">Payloads:
Payloads:
Payload#0
Payload#1
Line 930 ⟶ 1,208:
Missing in position:
Payload#10-pos2
Payload#16-pos5</pre>
 
</pre >
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import macros,sugar,strformat
#macro to take a nested tuple and return a type
#e.g. (int,(int,int))=>(string,(string,string))
Line 960 ⟶ 1,238:
let tplt2 = ((1,2),(3,4,1),6)
echo replace(tplt1, p)
echo replace(tplt2, p)</langsyntaxhighlight>
{{out}}
<pre>(("payload1", "payload2"), ("payload3", "payload4", "payload1"), "payload5")
Line 967 ⟶ 1,245:
=={{header|Perl}}==
Only handles nesting one level deep. Missing data is <code>undef</code> in the data structure, an empty string in the pretty-printer.
<langsyntaxhighlight lang="perl">sub fulfill {
my @payloads;
push @payloads, 'Payload#' . $_ for 0..5;
Line 983 ⟶ 1,261:
print formatted fulfill( [[1,2], [ 3,4,1], 5] );
print formatted fulfill( [[1,2], [10,4,1], 5] );
</syntaxhighlight>
</lang>
{{out}}
<pre>[ [ "Payload#1", "Payload#2" ], [ "Payload#3", "Payload#4", "Payload#1" ], "Payload#5" ]
[ [ "Payload#1", "Payload#2" ], [ "", "Payload#4", "Payload#1" ], "Payload#5" ]</pre>
 
===Arbitrary Nesting===
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Nested_templated_data
use warnings;
use Data::Dump 'dd';
 
my $t = [
[[1, 2],
[3, 4, 1],
5]];
my $p = [ map "Payload#$_", 0 .. 6 ];
dd { 'template' => $t, 'payload' => $p };
 
my $output = filltemplate( $t, $p );
dd { 'output' => $output };
 
sub filltemplate
{
my ($t, $p) = @_;
return ref $t eq 'ARRAY' ? [ map filltemplate($_, $p), @$t ] : $p->[$t];
}</syntaxhighlight>
{{out}}
<pre>
{
payload => [
"Payload#0",
"Payload#1",
"Payload#2",
"Payload#3",
"Payload#4",
"Payload#5",
"Payload#6",
],
template => [[[1, 2], [3, 4, 1], 5]],
}
{
output => [
[
["Payload#1", "Payload#2"],
["Payload#3", "Payload#4", "Payload#1"],
"Payload#5",
],
],
}
</pre>
 
=={{header|Phix}}==
Line 992 ⟶ 1,318:
 
Note that Phix indexes are normally 1-based, but to better match the task description those in the templates are 0-based
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">template</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #0000FF;">{</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span> <span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">},</span> <span style="color: #000000;">5</span> <span style="color: #0000FF;">}</span> <span style="color: #0000FF;">},</span>
<span style="color: #000000;">template2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #0000FF;">{</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span> <span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">},</span> <span style="color: #000000;">5</span> <span style="color: #0000FF;">}</span> <span style="color: #0000FF;">},</span>
Line 1,027 ⟶ 1,353:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Missing payloads: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">missing</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
<pre>
{{{"Payload#1", "Payload#2"},
Line 1,046 ⟶ 1,372:
 
A distinctive string is used to indicate missing payloads.
<langsyntaxhighlight lang="python">from pprint import pprint as pp
 
class Template():
Line 1,094 ⟶ 1,420:
print(" MISSING PAYLOADS:\n ", end='')
missed = t.missed_payloads
print('\n '.join(missed) if missed else '-')</langsyntaxhighlight>
 
{{out}}
Line 1,145 ⟶ 1,471:
This is a fairly straightforward implementation in R using a recursive function. It's not likely the most efficient in the world.
 
<langsyntaxhighlight Rlang="r">fill_template <- function(x, template, prefix = "Payload#") {
for (i in seq_along(template)) {
temp_slice <- template[[i]]
Line 1,168 ⟶ 1,494:
toJSON(payload, auto_unbox = TRUE),
toJSON(result, auto_unbox = TRUE)
))</langsyntaxhighlight>
 
{{out}}
Line 1,180 ⟶ 1,506:
<code>rackunit</code> is used to test the outcomes of template application. So no output indicates expectations met (i.e. success).
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define current-not-found-handler
Line 1,212 ⟶ 1,538:
 
(parameterize ((current-not-found-handler (λ (idx max) (format "?~a" idx))))
(check-equal? (out-of-bounds-generating-template-substitution p) '("?7"))))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,218 ⟶ 1,544:
{{works with|Rakudo|2020.08.1}}
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 Raku 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.
<syntaxhighlight lang="raku" perl6line>say join "\n ", '##PAYLOADS:', |my @payloads = 'Payload#' X~ ^7;
 
for [
Line 1,231 ⟶ 1,557:
say "\n Template: ", $_.raku;
say "Data structure: { @payloads[|$_].raku }";
}</langsyntaxhighlight>
{{out}}
<pre>##PAYLOADS:
Line 1,248 ⟶ 1,574:
Data structure: ((("Payload#1", "Payload#2"), (Any, "Payload#4", "Payload#1"), "Payload#5"),)</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, (((1 2) (3 4 1) 5)): e.Template
, "Payload#0" "Payload#1" "Payload#2" "Payload#3"
"Payload#4" "Payload#5" "Payload#6": e.Payload
= <Prout <Subst (e.Payload) e.Template>>;
};
 
Subst {
(e.P) = ;
(e.P) s.I e.X = <Item s.I e.P> <Subst (e.P) e.X>;
(e.P) (e.X) e.Y = (<Subst (e.P) e.X>) <Subst (e.P) e.Y>;
};
 
Item {
0 t.I e.X = t.I;
s.N t.I e.X = <Item <- s.N 1> e.X>;
};</syntaxhighlight>
{{out}}
<pre>(((Payload#1 Payload#2 )(Payload#3 Payload#4 Payload#1 )Payload#5 ))</pre>
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX */
tok.=''
Do i=0 To 6
Line 1,355 ⟶ 1,701:
End
Else
Return "'Payload#" i "not defined'"</langsyntaxhighlight>
{{out}}
<pre>Template [[[1,2],[3,4,1],5]]
Line 1,371 ⟶ 1,717:
 
===version 2===
<langsyntaxhighlight lang="rexx">/* REXX */
tok.=''
Do i=0 To 6
Line 1,470 ⟶ 1,816:
 
o: Say arg(1)
Return</langsyntaxhighlight>
{{out}}
<pre>[[[1,2],[ 3,4,1],5]]
Line 1,497 ⟶ 1,843:
Payload 8 is not defined</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program substitution;
template := [[[1,2],[3,4,1],5]];
payload := {
[0,"Payload#0"], [1,"Payload#1"], [2,"Payload#2"], [3,"Payload#3"],
[4,"Payload#4"], [5,"Payload#5"], [6,"Payload#6"]
};
 
print(subst(template, payload));
 
proc subst(template, payload);
if not is_tuple(template) then
return(payload(template));
end if;
 
result := [];
loop for item in template do
result with:= subst(item, payload);
end loop;
return result;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>[[['Payload#1' 'Payload#2'] ['Payload#3' 'Payload#4' 'Payload#1'] 'Payload#5']]</pre>
=={{header|VBA}}==
VBA allows arrays of variant, so the elements of an array can be both scalars as arrays of different sizes.
<langsyntaxhighlight lang="vb">Public Sub test()
Dim t(2) As Variant
t(0) = [{1,2}]
Line 1,527 ⟶ 1,897:
If Not q(i) Then Debug.Print p(i + 1); " is not used"
Next i
End Sub</langsyntaxhighlight>{{out}}
<pre>Payload#1, Payload#2
Payload#3, Payload#4, Payload#1
Line 1,533 ⟶ 1,903:
Payload#0 is not used
Payload#6 is not used</pre>
 
=={{header|Wren}}==
{{trans|Crystal}}
{{libheader|Wren-set}}
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "./set" for Set
import "./sort" for Sort
 
var withPayload // recursive function
withPayload = Fn.new { |template, payload, used|
return template.map { |item|
if (item is List) {
return withPayload.call(item, payload, used)
} else {
used.add(item)
return "'%(payload[item])'"
}
}.toList
}
 
var p = ["Payload#0", "Payload#1", "Payload#2", "Payload#3", "Payload#4", "Payload#5", "Payload#6"]
var t = [[[1, 2], [3, 4, 1], 5]]
var used = []
System.print(withPayload.call(t, p, used))
System.print()
var unused = Set.new(0..6).except(Set.new(used)).toList
Sort.insertion(unused)
System.print("The unused payloads have indices of %(unused).")</syntaxhighlight>
 
{{out}}
<pre>
[[['Payload#1', 'Payload#2'], ['Payload#3', 'Payload#4', 'Payload#1'], 'Payload#5']]
 
The unused payloads have indices of [0, 6].
</pre>
 
=={{header|zkl}}==
Line 1,539 ⟶ 1,944:
 
Void is used as a marker for an unknown payload.
<langsyntaxhighlight lang="zkl">var payloads=[1..6].pump(List,"Payload#".append);
 
fcn get(n){ try{ payloads[n - 1] }catch{ Void } }
fcn sub(list){ list.pump(List, fcn(n){ if(n.isType(List)) sub(n) else get(n) }) }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach p in (T(
T(T(T(1, 2),
T(3, 4, 1),
Line 1,551 ⟶ 1,956:
5),))){
println(" Template: %s\nData structure: %s".fmt(p,sub(p)));
}</langsyntaxhighlight>
{{out}}
<pre>
2,115

edits