OoBasic find value in an Array

Keyboard macros or custom scripts

OoBasic find value in an Array

Postby NzKb13 » Fri Nov 13, 2020 11:02 am

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   Expand viewCollapse view
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
NzKb13
 
Posts: 3
Joined: Fri Nov 13, 2020 10:28 am

Re: OoBasic find value in an Array

Postby Villeroy » Fri Nov 13, 2020 12:55 pm

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   Expand viewCollapse view
>>> 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
User avatar
Villeroy
Volunteer
 
Posts: 29722
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: OoBasic find value in an Array

Postby JeJe » Fri Nov 13, 2020 1:30 pm

binary search for sorted items

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

that one quickly modified for strings:

Code: Select all   Expand viewCollapse view
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


Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1537
Joined: Wed Mar 09, 2016 2:40 pm

Re: OoBasic find value in an Array

Postby NzKb13 » Fri Nov 13, 2020 3:32 pm

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
NzKb13
 
Posts: 3
Joined: Fri Nov 13, 2020 10:28 am

Re: OoBasic find value in an Array

Postby JeJe » Fri Nov 13, 2020 7:30 pm

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.
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1537
Joined: Wed Mar 09, 2016 2:40 pm

Re: OoBasic find value in an Array

Postby Villeroy » Fri Nov 13, 2020 8:04 pm

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
User avatar
Villeroy
Volunteer
 
Posts: 29722
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 7 guests