[Solved] Depiction of An Array of Arrays As String

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

[Solved] Depiction of An Array of Arrays As String

Post by sokolowitzky »

I use arrays a lot. They are useful especially in batch calculations.
But since I receive datas from various sources, the link between the resource and the element start becoming untraceable.
In such cases the best way is to display the array elements in print().
It's possible to display "Join(array(...))" if the array is consisted of strings/integers/doubles etc.
Once the array is consisted of subarrays It becomes harder to display it since it needs a lot of loops.
Could there be another method to display in msgbox an array like this one below? It's just an example to show different types of variants in multi dimensional arrays.
TheArray = Array(23,"07", Array(Array("TwentyThree", True),"07105106"))

I've looked for it in possible sources(pitonyak, Sun's programming guide etc) but maybe I don't use correct keywords.
Last edited by sokolowitzky on Thu Dec 01, 2022 5:29 am, edited 1 time in total.
Win10-OpenOffice 4.1/LibreOffice 7.4
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Depiction of An Array of Arrays As String

Post by JeJe »

Does MRI do what you want? With your example and MRI loaded add

Code: Select all

MRI thearray
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Depiction of An Array of Arrays As String

Post by Lupp »

It's a case for an iteration of recursive type.

Code: Select all

Sub tryItNowOnce()
expl = Array(23,"07", Array(Array("TwentyThree", True),"07105106"))
trans = Array() REM Initialies the transient variable.
appendJoinWithRecursion(trans, expl)
MsgBox(Ubound(trans))
REM Up to here the atomic elements can be of any type.
REM Next line expects strings.
MsgBox(Join(trans, "↕"))
REM This is easily used if you want to assure strings, and to have a function usable in Calc.
REM If your atoms are simple scalar values, you can, of course, save additional information about the type.
End Sub

Sub appendJoinWithRecursion (pHave, pThing)
REM Due to many Redim operations this may not be extremely efficient.
REM Two passes may not be better. Better ideas?
u= Ubound(pHave)
If NOT IsArray(pThing) Then
 u = u + 1
 Redim Preserve pHave(u)
 pHave(u) = pThing
Else
 l = Lbound(pThing) : u = Ubound(pThing)
 For j = l To u
  appendJoinWithRecursion(pHave, pThing(j))
 Next j
EndIf
End Sub
Suppose with Python it would only need one line?
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Depiction of An Array of Arrays As String

Post by sokolowitzky »

Herr Lupp,
Herzlichen Dank. It's a very inspiring approach.

I am planning to make some minor additions to your code before using it. But I did not finish it yet since I'm in a hurry to office(damn boundaries:)).
I can see your point on the fact that the code might be computationally consuming.
But I need it, only time to time, in order to check if my own project works without a problem so it should be sufficient this way.

Here below my own sketch. Probably some global variable will help to make it look like as I need.
Since the arrays I use have various number of elements I must see where the array begins and ends as well.
I am planning to finish this code below once I have some free time in a short while.

Code: Select all

Global AnAdditionalPrefix 
Global AnAdditionalSuffix
Global CurrentRecurrenceStatus

Sub tryItNowOnce()
expl = Array(23,"07", Array(Array(Array(Array("TwentyThree", True))),"07105106"))
 
trans = Array() REM Initialies the transient variable.
appendJoinWithRecursion(trans, expl)
MsgBox(Ubound(trans))
REM Up to here the atomic elements can be of any type.
REM Next line expects strings.
MsgBox(Join(trans, ";"))
REM This is easily used if you want to assure strings, and to have a function usable in Calc.
REM If your atoms are simple scalar values, you can, of course, save additional information about the type.
End Sub

Sub appendJoinWithRecursion (pHave, pThing)
REM Due to many Redim operations this may not be extremely efficient.
REM Two passes may not be better. Better ideas?
ll = lbound(pHave)
u= Ubound(pHave)
if isArray(pthing) = false then
msgbox IsArray(pThing) & oarrow & pthing & "; " & u
end if
If NOT IsArray(pThing) Then
 u = u + 1
 Redim Preserve pHave(u)
' msgbox CurrentRecurrenceStatus
 if CurrentRecurrenceStatus = "On" then
  pHave(u) = "Array(" & AnAdditionalPrefix  & pThing & AnAdditionalSuffix  & ")"'CurrentAdditionalPrefix = iif(j = l, "Array(", ",")
'CurrentAdditionalSuffix = iif(j = l, ")", "")
 Else
 pHave(u) = pThing'"Array(" & pThing & ")"
 End if
Else
 l = Lbound(pThing) : u = Ubound(pThing)
RecurrenceStatus
 For j = l To u

AddPrefix(j , l, "Array(", ",")
AddSuffix(j, u, ")", "")
  appendJoinWithRecursion(pHave, pThing(j))
 
 Next j
  RecurrenceStatus
EndIf
End Sub

Function RecurrenceStatus
If CurrentRecurrenceStatus = "On" Then
CurrentRecurrenceStatus = "Off"
Else
CurrentRecurrenceStatus = "On"
End If
End Function

Function AddPrefix(j, l, aprefix, alterprefix)
AnAdditionalPrefix = iif(j = l, aprefix, alterprefix)
End Function

Function AddSuffix(j, u, asuffix, altersuffix)
AnAdditionalSuffix = iif(j = u, asuffix, altersuffix)
End Function

Function oarrow
oarrow = "<=>" 
End Function

Win10-OpenOffice 4.1/LibreOffice 7.4
Post Reply