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
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.
>>> 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
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)
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
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)
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