A template for data is an arbitrarily nested tree of integer indices.

Data payloads are given as a separate mapping, array or other simpler, flat,
association of indices to individual items of data, and are strings.

The idea is to create a data structure with the templates' nesting, and the
payload corresponding to each index appearing at the position of each index.

Answers using simple string replacement or regexp are to be avoided. The idea is
to use the native, or usual implementation of lists/tuples etc of the language
and to hierarchically traverse the template to generate the output.

Task Detail

Given the following input template t and list of payloads p:

# 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.
t = [
    [[1, 2],
     [3, 4, 1], 
     5]]
 
p = 'Payload#0' ... 'Payload#6'
The correct output should have the following structure, (although spacing and
linefeeds may differ, the nesting and order should follow):

[[['Payload#1', 'Payload#2'],
  ['Payload#3', 'Payload#4', 'Payload#1'],
  'Payload#5']]
1. Generate the output for the above template, t.


Optional Extended tasks

2. Show which payloads remain unused.

3. Give some indication/handling of indices without a payload.


Show output on this page.

/* REXX */
tok.=''
Do i=0 To 6
  tok.i="'Payload#"i"'"
  End
t1='[[[1,2],[3,4,1],5]]'
t2='[[[1,6],[3,4,7,0],5]]'
Call transform t1
Call transform t2
Exit
 
transform:
Parse Arg t 1 tt
/* http://rosettacode.org/wiki/Nested_templated_data */
/*
[[['Payload#1', 'Payload#2'],
  ['Payload#3', 'Payload#4', 'Payload#1'],
  'Payload#5']]
*/
lvl=0
n.=0
o=''
w=''
used.=0
Do While t<>''
  Parse Var t c +1 1 c3 +3 1 c2 +2
  u=' '
  v=' '
  Select
    When c3='],[' Then Do
      o=o'  '
      w=w'  '
      t=substr(t,3)
      End
    When c2='],' Then Do
      o=o' '
      w=w' '
      t=substr(t,2)
      lvl=lvl-1
      End
    When c='[' Then
      lvl=lvl+1
    When c=']' Then
      lvl=lvl-1
    When c=',' Then
      Nop
    Otherwise Do
      u=lvl
      v=c
      End
    End
  t=substr(t,2)
  o=o||u
  w=w||v
  End
Say 'Template' tt
Do i=1 By 1 While w<>''
  If i=1 Then Do
    w=substr(w,4)
    p=pos('  ',w)
    Call o '[[['cont(left(w,p-1))'],'
    w=substr(w,p)
    End
  Else Do
    If left(w,3)='' Then Do
      w=substr(w,4)
      p=pos('  ',w)
      Call o '  ['cont(left(w,p-1))'],'
      w=substr(w,p)
      End
    Else Do
      w=substr(w,3)
      p=pos('  ',w)
      Call o '  'cont(left(w,p-1))']]'
      w=substr(w,p)
      End
    End
  End
Do i=0 To 6
  If used.i=0 Then Say 'Payload' i 'not used'
  End
Call o ' '
Return
 
o: Say arg(1)
   Return
 
cont: Procedure Expose tok. used.
  Parse Arg list
  res=''
  Do while list>''
    Parse Var list i list
    res= res tok(i)','
    End
  res=strip(res)
  res=strip(res,'T',',')
  Return res
 
tok: Procedure Expose tok. used.
Parse Arg i
If tok.i<>'' Then Do
  used.i=1
  Return tok.i
  End
Else
  Return "'Payload#" i "not defined'"