Convert 2D array to delimited string

Hello,

I have a sample array.

v = Array(Array("A","B","C","D"),"B","C",Array("A","B","C","D"),"E")

I would like to convert this to a nested and delimited string like this:

('A','B','C','D'),'B','C',('A','B','C','D'),'E'

Any help would be greatly appreciated. Thank you.

in Python:

v = (("A","B","C","D"),"B","C",("A","B","C","D"),"E")
print(repr(v))
"(('A', 'B', 'C', 'D'), 'B', 'C', ('A', 'B', 'C', 'D'), 'E')"

oops. sorry. i am programming in Basic. I will keep this in mind when i use python

I have got it but it still needs work:

Dim retVal As String
Sub doit()
	v = Array(Array("A","B","C","D"),"B","C",Array("A","B","C","D"),"E")
	join2(v,",")
	Print retVal
End Sub
sub join2(daAr As Variant, innerDelim$) As String
Dim x As Variant
	For Each x In daAr
		If vartype(x) =8204 then
			retVal  = retVal & "("& join(x,innerDelim) & "),"
		Else
			If x <> daAr(ubound(daAr)) Then
				retVal=retVal & x & innerDelim
			Else
				retVal=retVal & x 
			End if
		End if
	Next x
End sub

Thank you

As you can already see from the meaning of the word “string”, one “string” is always one string and therefore cannot be neither nested or delimited!

I’m afraid you want to reinvent the serialisation of data? … Please not like that!

Hello

I don’t want reinvent anything. If there is anything ready made that i can use (in Basic) please let me know. I did some searches with no success.

why do you insist on basic again, you have already understood that python is the more suitable tool.

Don’t make fun of people, you know they’ll copy it!

import json
print( json.dumps( v ))

I don’t think you can specify your idea/concept of a nested and delimited string made from your nested Array sufficiently by one single example. And you can’t leave the speciofication to the user tryinmg to help you.

Hallo

def frame_it(iterable):
    for entry in iterable:
        if isinstance(entry,(list, tuple)):
            yield f"({';'.join(frame_it(entry))})"
        else:            
            yield f"{entry}"

def main(*_):
    v = [["A","","","B"],"C","D",["E","F",[["G",""]]],"Z"]
    print(';'.join(frame_it(v)))

that prints:
(A;;;B);C;D;(E;F;((G;)));Z

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?

1 Like

Thank you foe your assistance. I will test this tomorrow.

Hello, this solution works for me. For those who suggested I do this in python. I agree, however I have to choose between what i can accomplish quickly vs. improving my skills. Currently I have to choose what i know better. Thank you.