OOBasic Split function changes Array type WARNING

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

OOBasic Split function changes Array type WARNING

Post 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)
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: OOBasic Split function changes Array type WARNING

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
echo8hink
Posts: 12
Joined: Wed Mar 02, 2016 7:59 pm

Re: OOBasic Split function changes Array type WARNING

Post 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
Attachments
BASIC Functions and Constants from LibreOffice source V3.6.2.2.ods
(26.5 KiB) Downloaded 263 times
LibreOffice 5.4.5.1 on Linux Mint 18.3
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: OOBasic Split function changes Array type WARNING

Post 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)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
arfgh
Posts: 566
Joined: Tue Mar 05, 2013 6:44 pm

Re: OOBasic Split function changes Array type WARNING

Post 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 ?
OpenOffice last version | Mageia Linux x64 | Ubuntu Linux | Windows 8.1 Enterprise x64 | Java last version
Post Reply