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.

Font "Courier New"
cls
Module Checkit {
      t=(((1,2), (3,4,1),5),)    ' use (1,) for one item tuple
      Tuple$ = lambda$ (a, feed$=" ") -> {
            \\ we can pass a tuple of two arguments or two arguments
            k=each(a)
            lim=len(a)-1
            res$="("
            link a to a() ' to use type$()
            while k {
                  if type$(a(k^))="mArray" then
                        res$+=Lambda$(array(a, k^),feed$+" ")
                        if k^<lim then
                              res$+={,
                              }+feed$
                        end if
                  else      
                        res$+= trim$(str$(array(k)))
                        if k^<lim then res$+=", "
                  end if
            }
            =res$+")"
      }
      TotalPayload = lambda (a)->{
            k=each(a)
            link a to a()
            res=0
            while k {
                  if type$(a(k^))="mArray" then
                        res+=Lambda(a(k^))
                  else
                        res++
                  end if
            }
            =res
      }
      Payload = lambda (a,payloads as list)->{
           misspayloads=List
           used=list
           inner=lambda misspayloads, p=1, payloads, used (a)-> {
                  k=each(a)
                  res=(,)
                  link a to a()
                  while k {
                        if type$(a(k^))="mArray" then
                             Append res, (Lambda(a(k^)),)
                        else
                              curpayload$="Payload#"+trim$(str$(array(k)))
 
                              if not exist(payloads, curpayload$) Then
                                    if not exist(used, curpayload$) Then
                                          Append res, ("missing#pos"+trim$(str$(p)),)
                                          append misspayloads, p:=curpayload$
                                    else                               
                                          Append res, (curpayload$,)
                                    End if
                                    p++
                              else
                                    Append res, (curpayload$,)
                                    if exist(payloads, curpayload$) then 
                                          delete payloads, curpayload$
                                          if not exist(used, curpayload$) then append used, curpayload$
                                    end if
                                    p++
                              end if
                        end if
                  }
                  =res
            }
            =inner(a), payloads, misspayloads
      }
      Expand$ =lambda$ (a as array, unused as list, misspayloads as list)-> {
            Read ? space$
            inner$= lambda$ (a, feed$=" ")->{
                  k=each(a)
                  lim=len(a)-1
                  res$="["
                  link a to a() ' to use type$()
                  while k {
                        if type$(a(k^))="mArray" then
                              res$+=Lambda$(array(a, k^),feed$+" ")
                              if k^<lim then
                                    res$+={,
                                    }+feed$
                              end if
                        else      
                              res$+= "'"+array$(k)+"'"
                              if k^<lim then res$+=", "
                        end if
                  }
                  =res$+"]"      
            }
            document unused$="Unused Payloads"+{
            }
            if len(unused)>0 then
                  un=each(unused)
                  while un {
                        unused$="  "+eval$(un)+{
                        }
                  }
            else
                  unused$="  -"
            end if
            if len(misspayloads)>0 then
                  un=each(misspayloads)
                  lim=len(misspayloads)-1
                  document missing$="Missing in position: "+{
                  }
                  while un {
                        missing$="  "+eval$(un)+"-pos"+eval$(un,un^)+{
                        }
                  }
                  =inner$(a, space$)+{
                  }+unused$+missing$
            Else
                  =inner$(a, space$)+{
                  } + unused$
            End if
      }
      flush
      Data t, (((1,10), (3,4,16),5),)
      While not empty {
            Read t
            Document result$="Payloads:"+{
            }
            p=list
            for i=0 to 6 {
                  Append p, "Payload#"+trim$(str$(i))
                  result$="  "+eval$(p, i)+{
                  }
            }
            result$="Template:"+{
            }
            result$="  "+Tuple$(t, "   ")+{
            }
            result$="Template with Payloads:"+{
            }
            m=Payload(t, p)
            result$="  "+Expand$(!m, "   ")
 
            clipboard result$
      }
 
}
Checkit
Report clipboard$