OOBasic Split function changes Array type WARNING

Creating a macro - Writing a Script - Using the API

OOBasic Split function changes Array type WARNING

Postby JeJe » Wed Oct 31, 2018 10:06 pm

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   Expand viewCollapse view
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)
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1088
Joined: Wed Mar 09, 2016 2:40 pm

Re: OOBasic Split function changes Array type WARNING

Postby JeJe » Thu Nov 01, 2018 12:10 am

This works:

Code: Select all   Expand viewCollapse view
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.
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1088
Joined: Wed Mar 09, 2016 2:40 pm

Re: OOBasic Split function changes Array type WARNING

Postby echo8hink » Sat Nov 03, 2018 10:54 pm

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 69 times
LibreOffice 5.4.5.1 on Linux Mint 18.3
echo8hink
 
Posts: 10
Joined: Wed Mar 02, 2016 7:59 pm

Re: OOBasic Split function changes Array type WARNING

Postby JeJe » Sat Nov 03, 2018 11:19 pm

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   Expand viewCollapse view
dim a as long
a =554555
b= split(a,"4")
msgbox b(0)
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1088
Joined: Wed Mar 09, 2016 2:40 pm

Re: OOBasic Split function changes Array type WARNING

Postby arfgh » Wed Jul 01, 2020 12:29 pm

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 | Windows 8.1 Enterprise x64 | Windows XP Pro x64 SP2 | Java 1.8.0_231
arfgh
 
Posts: 517
Joined: Tue Mar 05, 2013 6:44 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests