"True 2D" arrays vs "1D of 1D-same-length"

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

"True 2D" arrays vs "1D of 1D-same-length"

Post by Lupp »

The array properties of cell ranges (.DataArray e.g.) are organized as 1D-arrays (like a vector subscripted top down) of sections of rows (equally dimensioned vectors subscripted left to right). All indices 0-based.
I don't know a practical way to organise data being semantically 2D in a way to be accessible syntactically as "1D of 1D-same-length" in Basic.
I also don't know a way to easily convert between these structures.

Do you?
(I only know complicated and inefficient/doubtable workarounds.)

A special case in pseudo notation: cellRange.GetDataArray(myArray) where myArray can be a true 2D array

Background:
-1- "True 2D" and "1D of 1D-same-length" both have their advantages and disadvantages regarding different use-cases.
-2- For my personal toolbox I wrote some functions for the analysis and the manipulation of arrays. As long as I cannot convert "1D of 1D-same-length" to "True 2D" and back I would need a second version of any of those functions for the array-property case.
-3- A workaround assigning the inner elements one-by-one would be inefficient in time and in RAM usage. It would also only be available for one direction. ...
-4- There should be a simple solution: cellRange.SetDataArray(myArray) is working as expected with a true 2D array myArray. (I take this as an evidence that the memory mapping is the same for both the array types.)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: "True 2D" arrays vs "1D of 1D-same-length"

Post by Villeroy »

Nested 1D arrays are supported by any programming language.

This was one of my first StarBasic macros some 15 years ago when I struggled with list boxes and cell ranges. Meanwhile the Base component works well enough that I don't need this anymore.

Code: Select all

'convert a 2-dimensional, 1-based plain array (as passed by an array function of a cell)
'to a 1 dimensional, zero-based array of rows with nested arrays of columns (as returned by oRange.getDataArray())
Sub PlainArray2DataArray(a())
dim aCol(),aRow(), i%,j%,iUB1%,iUB2%
iUB1 = uBound(a(),1)
redim aRow(iUB1 -1)
iUB2 = uBound(a(),2)
for i = 1 to iUB1
   redim aCol(iUB2 -1)
   for j = 1 to iUB2
      aCol(j -1) = a(i,j)   
   next
   aRow(i -1) = aCol()
next
a() = aRow()
End Sub
'the opposite of the above
Sub DataArray2PlainArray(aRows())
Dim i%,j%,aCols(),aTmp()
redim aTmp(1 to uBound(aRows()) +1,1 To uBound(aRows(0)) +1)
for i = 0 to uBound(aRows())
   aCols = aRows(i)
   for j = 0 to uBound(aCols())
      aTmp(i +1,j +1) = aCols(j)
   next
next
aRows() = aTmp()
End Sub
Last edited by Villeroy on Fri Mar 08, 2024 12:16 pm, edited 1 time in total.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: "True 2D" arrays vs "1D of 1D-same-length"

Post by Lupp »

Thanks @Villerroy.

However, disregarding a few details the suggested solution is what I had in mind as "inefficient and complicated workaround". The "complicated" did not refer to the strucure of the code but to the necessity to assign the values one by one, an to keep -at least as intermediary - the data physically in parallel in two variables often with independent data containers.

To have to do so is annoying. It may also be unnecessarily inefficient as I conclude (guess?) from my statement under
-4- "... I take this as an evidence that the memory mapping is the same for both the array types".
In other words: I might problay only need to to find two different ways for accessing the same data to avoid any physical movement. 
In my preferred general programming language Pascal alternative access to the same chunk of data in memory can be organised by declaring a second variable "on top" of the first one by using the absolute clause.

Thus I am still intersted to ...
A) find a way to access the same elements by either a(i,j) or by b(i)(j) of my choice.
or
B) get a convincing refutation of the feasibility by an explanation of the actual memory mapping (including indirections).
B) may reduce to an explanation where the dimension information is saved in RAM. In this case I would also like to learn how .SetDataArray(plain2Darray) works.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply