[Solved] Is it possible to access an array using one of its elements

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] Is it possible to access an array using one of its elements

Post by sokolowitzky »

I have searched it in the forum and in the Pitonyak but I could not see anything similar. That's why I'm asking.
Let's say I have an array as ParentArray1 = Array(0,1,2,3) and defined one of its elements as aunit = ParentArray1(1).
So the numbers given in parantheses give us elements of an array. I am wondering if the reverse is possible.
Is it possible to make a function, or is there any service to take control of ParentArray1 using aunit.
As an example of use, is it possible to make something like this one below.

Code: Select all

Function ParentArray(aunit)
ParentArray(aunit) = ParentArray1
End Function
Last edited by MrProgrammer on Wed Aug 10, 2022 11:48 pm, edited 6 times in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Is it possible to access an array using one of its elements

Post by Zizi64 »

parentarray1 = Array(0,1,2,3) and defined one of elements as aunit = oarray(1).
Is the name "oarray " a typo?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Is it possible to access an array using one of its elements

Post by sokolowitzky »

Yes, sorry. I fixed it now. It was supposed to give a reference to ParentArray1 that I have menionned formerly.
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Is it possible to access an array using one of its elements

Post by Zizi64 »

Do you want to choose one array from many known, existing arrays by its element?

How many arrays you have? And are there same elements is two arrays in same position? If the answer is "yes": how you can decide which array will be chosen?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Is it possible to access an array using one of its elements

Post by sokolowitzky »

According to your post, I understand that, sub elements have no properties that might help understand if they are simple item or if they are an element of an array.
So the only way to do it is comparing the single(string, integer, double etc) objects and elements of array?
Win10-OpenOffice 4.1/LibreOffice 7.4
Jan_J
Posts: 167
Joined: Wed Apr 29, 2009 1:42 pm
Location: Poland

Re: Is it possible to access an array using one of its elements

Post by Jan_J »

Shlightly off-topic

It depends on what you expect. There is no mechanism that let the substructure to know address of the larger structure from which it has been taken. However the problem can be reversed.

If you are thinking about identyfying location of unique value in the array; then it reduces to problem of searching the value in an array.

More general approach is to refere directly to the selected piece of larger structure. This is how many modern languages work. E.g. in Python refernce is the most common mechanism of sharing a value. In Basic each = assignmet with a structure (like an array) on the right hand side also creates a reference. So,

a = array(2,3,5,8)
b = a(0)

makes a copy of the first element of a, while

a = array(array(2,3),array(5,8))
b = a(0)

makes a reference to the first subarray.
Any changes made to b are then visible in a.

This behaviour is language-dependent.
Last edited by Jan_J on Tue Aug 09, 2022 11:49 pm, edited 1 time in total.
JJ ∙ https://forum.openoffice.org/pl/
LO (7.6) ∙ Python (3.11|3.10) ∙ Unicode 15 ∙ LᴬTEX 2ε ∙ XML ∙ Unix tools ∙ Linux (Rocky|CentOS)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Is it possible to access an array using one of its elements

Post by JeJe »

sokolowitzky wrote: Mon Aug 08, 2022 7:32 am According to your post, I understand that, sub elements have no properties that might help understand if they are simple item or if they are an element of an array.
So the only way to do it is comparing the single(string, integer, double etc) objects and elements of array?
The vartype function will tell you if an item is an array - if so the return will include the array constant value (8192)

Edit: though that's probably not what you're asking.

Edit2: The only way to pass an array item to a function (as opposed to another variable set to the same value) is by knowing the array already.
if you say a bit more about what you're trying to do maybe someone will have a simple answer for you.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Is it possible to access an array using one of its elements

Post by sokolowitzky »

Now I can see that my question was somehow baseless.

Since array(0) calls first element of an array, I thought maybe there could be a way to call the array using one of its items like this, I'm completely speculating.

array(0) = unit
unit^-1 = array()
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Is it possible to access an array using one of its elements

Post by Zizi64 »

array(0) = unit
unit^-1 = array()
A simple value can not store information about the source: where it came from.

For example there is a result 10 - when you have information about this result is come from a two integer numbers adding: you can not know if they was 1+9, or 2+8, or 3+7... etc...
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Is it possible to access an array using one of its elements

Post by JeJe »

You seem to be thinking or wanting that when you put item=array(0) then item is a reference to array(0). It isn't - its a variable with the value of array(0)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Is it possible to access an array using one of its elements

Post by JeJe »

You can do something similar to what you want with a collection object and a user type which stores a reference to the collection along with the value.

Code: Select all

private type refandvalue
collectionref as object
itm as variant
end type

Sub Main
dim cc as new collection
dim a as new refandvalue
a.itm= 60
a.collectionref = cc
dim b as new refandvalue
b.itm= 50
b.collectionref = cc

cc.add a,"1",1
cc.add b,"2",2

msgbox cc.item(1).itm
cc2 =getcollectionfromitm(cc.item(1))
msgbox cc2.item(2).itm

End Sub

function getcollectionfromitm(c)
getcollectionfromitm= c.collectionref
end function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Is it possible to access an array using one of its elements

Post by JeJe »

And this *seems* to work using class modules

Create a module called ClassM and add this code

Code: Select all

option compatible
option classmodule

private cvalue
private rootarray
Sub setvalue(v)
cvalue = v
End Sub

function getvalue()
getvalue = cvalue
end function

function getrootarray()
 getrootarray =  rootarray
end function

sub setrootarray(ra)
rootarray = ra
end sub

Create another module with this code and run it.

Code: Select all



Sub Main
redim arrays(3)

for i = 0 to 3
arrays(i)= new classm
with arrays(i)
.setvalue(i)
.setrootarray arrays
end with
next


item= arrays(2)
msgbox item.getvalue
t= item.getrootarray
msgbox t(2).getvalue
arrays(2).setvalue(9)
msgbox t(2).getvalue

End Sub
Edit: removed surplus line
Last edited by JeJe on Tue Aug 09, 2022 6:44 pm, edited 1 time in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Is it possible to access an array using one of its elements

Post by Lupp »

(I probably didn't study some of the posts to every detail. My comment may therefopre be unsatisfactory.
From what I suppose to have understood:)

What the OQer conceptually seems to long for I would call a "BackwardEnhanced ContentAddressedMemory(CAM)", thus abbreviated BECAM.
CAM is a long discussed concept, but I don't know efficient implementations based on ordinary computer architecture. Any implementation will require anyway full knowledge of and full access to the allocated memory. Surely Basic doesn't provide this.
ECAM in the sense I see it here would in addition require a backward mapping of memory units to the universe of used variables. Again Basic is supposed to not come with any support for the kind of task.

However, you can create a UserDefinedMemoryManagement (UDMM) for a collection of variables and search them for content based on user code. The UserControlledMemory might then be represented either by a 1-D array (Basic???) of variable names and variable contents (references to contents) with a structure exemplified by UCM = Array(Array("myControlledVar1", myControlledVar1), Array("...", ...), ...) where simple variables would need to be restricted to assignment via this system because they not are assigned as references. Having found a specific content you can add the needed information (the variable, probably indices, ... and the name of the variable) to a list of findings (NOT unique!). In special cases clever usage of API objects may help.

Have a lot of fun with programming - and with waiting for the results in every single case of applying your BECAM system.
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: Is it possible to access an array using one of its elements

Post by sokolowitzky »

Dear Jeje, Dear Lupp, Dear Zizi,
I am a noob by nature :) I try to use ooo in unorthodox ways according to my needs, which are mostly about strings. But arrays help me managing datas easier.
To solve this problem I've been using a merely amateur way, which is giving each array's first value a unique name so ooo can search the correct one in many arrays. But this requires a lot of pc power in the background especially if there are 20 30 or more arrays.
Even though I realized my aim was not a proper use of ooo due to its nature, Jeje helped me exploring a different area.
Thank you all.
Win10-OpenOffice 4.1/LibreOffice 7.4
Post Reply