OoBasic find value in an Array

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
NzKb13
Posts: 3
Joined: Fri Nov 13, 2020 10:28 am

OoBasic find value in an Array

Post by NzKb13 »

Hi every body.
I'm looking for a way to check if a value is in an Array.
to do some think like that :

Code: Select all

Sub testarray
dim ceci(6) as String
ceci(0)="aa"
ceci(1)="bb"
ceci(2)="cc"
ceci(3)="dd"
ceci(4)="ee"
ceci(5)="ff"
x="cc"
MsgBox(ceci(2))
	if x in ceci then ' or some thing like that
		Msgbox("ok")
	Else
		MsgBox("non")
	End If
End Sub
of course i can use a loop to check on by on but is there a more efficient way?
I spent some time on the web but was unable to find. I guess it obvious but, I'm a kind of beginner...
thanks for help.
Paul
OpenOffice 6.46.2 / Ubuntu 20.04LTS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OoBasic find value in an Array

Post by Villeroy »

StarBasic is an insufficient programming language, particularly when it comes to arrays and strings. StarBasic was added to the StarOffice suite when MS came up with the first version of VBA in 1995. StarOffice was the predecessor of OpenOffice. Python is a rather usable alternative macro language with some other pitfalls. At least Python is a mature programming language.

Code: Select all

>>> a=('aa','bb','cc','dd')
>>> help(a)

>>> 'cc' in a
True
>>> a.__contains__('cc')
True
>>> 
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
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: OoBasic find value in an Array

Post by JeJe »

binary search for sorted items

https://www.freevbcode.com/ShowCode.asp?ID=9416

that one quickly modified for strings:

Code: Select all

Sub testarray
dim ceci(6) as String
ceci(0)="aa"
ceci(1)="bb"
ceci(2)="cc"
ceci(3)="dd"
ceci(4)="ee"
ceci(5)="ff"
x="cc"
msgbox arrayfind(ceci,x)

End Sub


Function arrayFind(theArray(), target) As Boolean
    Dim low As Integer
    low = 0
    Dim high As Integer
    high = UBound(theArray)
    Dim i As Integer
    Dim result As Boolean
    
    Do While low <= high
        i = (low + high) / 2
        If target = theArray(i) Then
            arrayFind = True
            Exit Do
        ElseIf target < theArray(i) Then
            high = (i - 1)
        Else
            low = (i + 1)
        End If
    Loop
    
    If Not arrayFind Then
        arrayFind = False
    End If
End Function


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
NzKb13
Posts: 3
Joined: Fri Nov 13, 2020 10:28 am

Re: OoBasic find value in an Array

Post by NzKb13 »

thanks Jeje, the function is fine.
Villeroy, you're right, Oobasic is not really done for that, I agree, Python is better.
I just wanted to have something 'simple' working in Calc-openoffice for my children to learn vocab and me to learn some Oobasic. I agree, like VBA it's not the best but can make some good simple job.
I leave the post open a little more in case...
Thanks
Paul
OpenOffice 6.46.2 / Ubuntu 20.04LTS
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: OoBasic find value in an Array

Post by JeJe »

If its a sorted array, its a binary search in whatever language you use. OOBasic doesn't have that built in, but you can find it very easily by putting VB6 and binary search into a search engine. OOBasic is similar to VBA and VB6 in these things. There is a huge free code base out there in VB6 - you don't need to reinvent the wheel, you copy and paste a function. Its not that much more difficult than it being built in.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OoBasic find value in an Array

Post by Villeroy »

You have spreadsheet functions doing lookups. You even have a database at hand. Anything text related is a typical database task. Calc is mainly an arithmetic program. I would not write Basic programs on top of an arithmetic program running on a Linux computer when I only need to do "learn vocab".
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
Post Reply