Page 1 of 1

OOBasic Split function changes Array type WARNING

Posted: Wed Oct 31, 2018 10:06 pm
by JeJe
Another annoying OOBasic quirk...

The split function changes a string array to a variant array - which means if you later redim preserve what you thought was a string array you lose all your data.

Code: Select all

SUB MAIN
DIM ST AS STRING,STS() AS STRING
st=  "A" & Chr(0) & "N" & Chr(0) & "D"

msgbox vartype(sts) '8200 = 8192 for array + 8 for string

sts= split(st,chr(0))

msgbox vartype(sts) '8200 = 8192 for array + 12 for VARIANT

for i = 0 to UBOUND(STS)
msgbox sts(i)
next

redim preserve sts(2)

msgbox vartype(sts) '8200 = 8192 for array + 8 for string BUT DATA IS LOST

for i = 0 to UBOUND(STS)
msgbox sts(i)
next

END SUB

An alternative is to use someone else's split function. There are a lot around as this is Basic - eg this one

http://www.vb-helper.com/howto_vb5_split_join.html

Its 1 based though. You'll have to modify it slightly to be zero based (putting num_items = -1 before the loop and
ReDim Preserve result(num_items) inside it)

Re: OOBasic Split function changes Array type WARNING

Posted: Thu Nov 01, 2018 12:10 am
by JeJe
This works:

Code: Select all

SUB MAIN
DIM ST AS STRING,STS() AS STRING
st=  "A" & Chr(0) & "N" & Chr(0) & "D"

sts= split(st,chr(0))

sts=RestoreStringArray(sts)

redim preserve sts(2)

for i = 0 to UBOUND(STS)
msgbox sts(i)
next

END SUB


function RestoreStringArray(Varray) 
dim tmp() as string,ub as long
ub = ubound(Varray)
if ub >-1 then
redim tmp(ub)
for i = 0 to ub
tmp(i)=Varray(i)
next
end if
RestoreStringArray=tmp
end function

Edit: slight mod to handle ubound = -1

Edit2: Or declaring sts() as variant to begin with.

Re: OOBasic Split function changes Array type WARNING

Posted: Sat Nov 03, 2018 10:54 pm
by echo8hink
This is a very interesting observation. I find it amusing that if you had not declared a type for the array, it would have worked just fine.

I have attached a spreadsheet file that I cannot find anywhere else. I have found it useful in situations like this (not now, though). I think I downloaded it from the old LibreOffice forum. It is a listing of functions and constants available in office BASIC. It was supposed to have been built from the source code to Star/OO/LOBASIC, if I remember correctly. This reference shows the "Split()" function returning "Object" type.

My best OOBasic guide says it returns a "String" array. It even gives a code example splitting and joining various strings. The example doesn't declare a type and never tries to "ReDim Preserve" the array.

The OOBasic reference I mentioned above is: OpenOffice.org BASIC, Simplified Reference, Author: Malcolm Ripley, Issue: 1, Date: 11th September 2007. I think it is available here from a link in the Macro help message... http://www.ripleyhome.com/toolbox.html

Your program clearly shows it will flip and return a "Variant". So, is it a bug or a feature? :ucrazy:

Thanks for the info. A concise source for this kind information would be very nice to have.

-hink

Re: OOBasic Split function changes Array type WARNING

Posted: Sat Nov 03, 2018 11:19 pm
by JeJe
I wasn't amused spending so many hours working out where the problem was. I've just tried declaring the array as different data types - long, byte whatever... and Split turns them all into variant arrays. Not an object so that documentation isn't reliable either.

Of course it shouldn't do this. This also runs giving 55:

Code: Select all

dim a as long
a =554555
b= split(a,"4")
msgbox b(0)

Re: OOBasic Split function changes Array type WARNING

Posted: Wed Jul 01, 2020 12:29 pm
by arfgh
The same happens using Array() function. Very curious in fact.

But the interesting question here is, is it faster to handle a Variant Array than a String Array, to get rid of it ?