[Solved] Calc Basic Macro - ReDim inside an array

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
msheliga1
Posts: 5
Joined: Fri Nov 05, 2021 7:20 pm

[Solved] Calc Basic Macro - ReDim inside an array

Post by msheliga1 »

AOO 4.1.10 - Windows 10 Safe Mode

Doing some calc AOO basic programming. I wish to create an array of arrays (a multi-dimesional array will not suffice, and
speed is also an issue so copying array cells one by one is not a good solution.)

Code: Select all

 
Dim oNewArrays()  ' This should be an array containing other 2D-arrays
Dim oNewArray()   ' The singular of the above, oNewArray() might equal oNewArrays(7), for example
' Calculate iFiles to Track which may be near 12 ... 
' Also calculate LGroupNewEndRow and iTotalCols

ReDim oNewArrays(0 to iFilesToTrack)  
' Now we wish to make each oNewArrays(index) contains 2D arrays
for For iGroup = 1 to iFilesToTrack
        ' ReDim oNewArrays(iGroup) (0 to5, 0 to 5)  ' This and similar statements with oNewArray(iGroup) all fail
        ' The following runs, but only the last value is correct .. the others are overwritten
	ReDim oNewArray(0 to LGroupNewEndRow, 0 to iTotalCols) 
	oNewArrays(iGroup) = oNewArray
        oNewArray(1, 1) = "This only shows up for the last iGroup of " & iGroup  ' Others are overwritten by ReDim oNewArray ....
Next iGroup


I've also tried the following

Code: Select all

 
' Same as above up to the loop ......

for For iGroup = 1 to iFilesToTrack
	oNewArray = oNewArrays(iGroup) 
	ReDim oNewArray(0 to LGroupNewEndRow, 0 to iTotalCols) 
        ' This leads to a "Cannot coerce argument type during co-reflection call" error below
        '  ... various processing setting oNewArray values, and setting oRange ....
       	oRange.setDataArray(oNewArray)
Next iGroup



If iFilesToTrack is small I can declare separate variables oNewArray1, oNewArray2, .... oNewArray12, reDim each and set
oNewArrays(1) to oNewArray1, oNewArrays(2) to oNewArray2, etc., but this is cumbersome and limited.
Last edited by msheliga1 on Thu Dec 30, 2021 8:17 pm, edited 2 times in total.
Michael Sheliga Open Office 4.1.10 on Windows 10 Home Version 20H2 - HP: LAPTOP-L7QUGG48
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: Calc Basic Macro - ReDim inside an array

Post by JeJe »

Code: Select all



sub test
Dim oNewArrays(10) 
for i = 0 to 10
oNewArrays(i) = getarray(100)
next
oNewArrays(5)(5) =100
msgbox oNewArrays(5)(5) 
end sub

function getarray(ub)
dim arr(ub)
getarray=arr
end function
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: Calc Basic Macro - ReDim inside an array

Post by Lupp »

I think you missed to emphasize the actual issue. "... all failed" isn't clear enough to me.
Well, you can Dim an array without an actual index range Dim sequ() and get a sequence of 0 entries (0 To -1, you may say). Neither is allowed a related construct for 2D arrays (or higher), nor can a Redim statement change the number of index positions (the dimensionality).
Therefore you need to decide if you can work through the complete task with sequences (1D arrays, Lbound = 0), or if you start with (say)

Code: Select all

Dim myArray2D(-1 To 0, -1 To 0)
and later redim for actual use by

Code: Select all

Redim (Preserve?) myArray2D(0 To newU1, 0 To newU2)
or similar
msheliga1 wrote:...and speed is also an issue so copying array cells one by one is not a good solution.
Did you already find viewtopic.php?t=93610 ?
The solution there relies on an implicit "deep copy" for an ordinary array by compiled routines of the application, and you don't need to organize the nested loops in 200 times slower Basic code. However, I don't know a specification assuring this behaviour. (In LibO there once was a related "bug" for a short time making the suggested user code fail.)

For arrays being on any level elements of an array you apply the function 'createArrayCopy()' to, the internal assignment will still be ByRef, however, as is standard for arrays. You need to go to any such elements of array type one by one therefore.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
msheliga1
Posts: 5
Joined: Fri Nov 05, 2021 7:20 pm

Re: Calc Basic Macro - ReDim inside an array

Post by msheliga1 »

Thanks Folks, :bravo:

what JeJe suggested - creating the inner array inside of a function - worked. (It also occurred to me in the middle of the night.) So simple yet I had a hard time coming up with it. Of course the function must return a Variant.

Thank you also, Lupp. :super: While viewtopic.php?t=93610 ? is similar, it deals with deep copying of a single array - without a loop. The problem in my code was that ReDim-sioning the inner array inside of a loop does not work, as the innerArray address is the same when assigned to NewArray(index). By putting the ReDim (or even just Dim) inside a method, a different array is returned each time.
Michael Sheliga Open Office 4.1.10 on Windows 10 Home Version 20H2 - HP: LAPTOP-L7QUGG48
Post Reply