This is a typical case where a recursive function should be used.
I didn’t wait for a more precise explanation of the task, but created a possible solution and applied it to an example showing at least that the handling of empty elements must be specified. Another issue is the handling of missing elements (err448) in the input sequence.
Sub doit2()
v = Array(Array("A","","","B"),"C","D",Array("E","F",Array(Array("G",""))),"Z")
w = nestingJoin(v, ";", "(", ")")
End Sub
Function nestingJoin(pSequence, pSep As String, pLNstr As String, pRNstr As String) As String
nestingJoin = ":fail:"
On Local Error Goto fail
REM the bottom level elements of pSequence musr be of types automatically converted to strings
REM if occurring ina concatenating expression. On higher levels sequences are allowed.
out = ""
u = Ubound(pSequence)
For j = 0 To u
j_element = pSequence(j)
If IsArray(j_element) Then
j_element = pLNstr & _
nestingJoin(j_element, pSep, pLNstr, pRNstr) & _
pRNstr
pSequence(j) = j_element
EndIf
Next j
nestingJoin = Join(pSequence, pSep)
fail:
End Function
Just to clarify: The questioner’s original example was not “2D
” at all, but showed a distorted arbitrary nesting where the depth was limited to 2 (1-based). My solution makes no restrictions here.
However, functions of certain programming systems treat 2D arrays as nested sequences of equal length. Also structures of the LibreOffice API do that. Nonetheless we should regard the distinction.
BTW: Does somone of the old wise men(f/m) here know if there is a programming system supporting “transparent” addressing of elements of nD arrays both the nested-sequences-way
and the listed-indices-way
, and probably even in mixed ways?