[Solved] Basic - Subset of an array

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
DCTN54
Posts: 10
Joined: Wed May 10, 2023 9:27 am

[Solved] Basic - Subset of an array

Post by DCTN54 »

Hi everybody,
How do I retrieve a subset from an array into a new array in ooo basic ?
Let's say, I have my_array(60), and I want my_new_array(20) as a subset of my_array with only values from 10 to 29 ?
Thanks for your help.
Last edited by DCTN54 on Fri May 12, 2023 7:47 am, edited 1 time in total.
LibreOffice 5.4.7.2.M6 on Windows 10
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Basic - subset of an array

Post by Zizi64 »

Have you tried the For .. Next cycle???
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Basic - subset of an array

Post by karolus »

Hallo

Code: Select all

…
new_data = data[10:30]
…
But, sorry, thats python, far beyond your capabilities!
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Basic - subset of an array

Post by JeJe »

First off do a search for a ready made sub or function so you don't have to reinvent the wheel. Sometimes searching for a VB6 function will find it, which is similar to OOBasic, and there's a bigger code base out there.

If not, its a trivial one to write - a suggested above, in OOBasic you have to use a loop.


Code: Select all


Sub Main
dim  arr(60)
arr(10) =60
arr(29) =78

msgbox getsubarray( arr,10,29,arrnew)
msgbox arrnew(0)
msgbox arrnew(19)
End Sub


Public Function GetSubArray(arr, ByVal starti As Integer, ByVal endi As Integer,arrnew) as boolean
dim i, c
on error goto hr
if endi<starti or  starti <lbound(arr) or endi > ubound(arr) then exit function
redim arrnew(endi - starti)
for i = starti to endi
arrnew(c) = arr(i)
c=c+1
next

GetSubArray = true
hr:
End Function

Edit: added defining variables - always a good idea,
Edit: corrected error doing that!
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Basic - subset of an array

Post by Villeroy »

You are not limited to stupid Basic. You can use Python as macro language. Python has all the string and array functions of a mature programming language and a lot more.
In Python b = a[3:9] writes elements 3 to 9 from array a to array b.
JeJe wrote: Thu May 11, 2023 5:17 pm First off do a search for a ready made sub or function s
For instance

Code: Select all

REM very simple routine appending some element to an array which can be undimensioned (LBound > UBound)
Sub bas_PushArray(xArray(),vNextElement)
Dim iUB%,iLB%
	iLB = lBound(xArray())
	iUB = uBound(xArray())
	If iLB > iUB then
		iUB = iLB
		redim xArray(iLB To iUB)
	else
		iUB = iUB +1
		redim preserve xArray(iLB To iUB)
	endif
	xArray(iUB) = vNextElement
End Sub
Push elements 3 to 9 of array a() into array b()

Code: Select all

dim b()
for x = 3 to 9
   bas_PushArray(b(), a(x))
next x
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
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Basic - subset of an array

Post by JeJe »

There are of course many variations for dressing it up even for such a simple task.

If the start index is 0 then you can of course just use redim preserve, so catering for that in mine:

Code: Select all

Public Function GetSubArray(arr, ByVal starti As Integer, ByVal endi As Integer,arrnew) as boolean
	dim i,c
	on error goto hr
	if endi<starti or  starti <lbound(arr) or endi > ubound(arr) then exit function
	if starti =0 then
		arrnew = arr
		redim preserve arrnew(endi)
	else
		redim arrnew(endi - starti)
		for i = starti to endi
			arrnew(c) = arr(i)
			c=c+1
		next
	end if
	GetSubArray = true
hr:
End Function
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: Basic - subset of an array

Post by JeJe »

@Villeroy

Redim preserve is very expensive (Edit: in a loop). Here's a timing test.

(Python I presume would be much quicker.)

Code: Select all

dim a
dim b(1000)
b(3) = 555
b(900) = 66
n = getsystemticks


for x = 3 to 900
   bas_PushArray(b(), a(x))
next x

n = getsystemticks - n
msgbox n '563


n = getsystemticks
res =getsubarray( b,3,900,arrnew)

n = getsystemticks - n
msgbox n '31


Last edited by JeJe on Thu May 11, 2023 6:42 pm, edited 1 time in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DCTN54
Posts: 10
Joined: Wed May 10, 2023 9:27 am

Re: Basic - subset of an array

Post by DCTN54 »

But, sorry, thats python, far beyond your capabilities!
Reading the subject is far beyond your capabilities it seems :shock: .
I asked about Basic because obviously in Python it is something very easy to do but not in Basic.
LibreOffice 5.4.7.2.M6 on Windows 10
DCTN54
Posts: 10
Joined: Wed May 10, 2023 9:27 am

Re: Basic - subset of an array

Post by DCTN54 »

Thanks to all for your suggestions it seems I reached the limit of Basic, if you need to set up a loop for something this simple...
LibreOffice 5.4.7.2.M6 on Windows 10
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Basic - subset of an array

Post by JeJe »

It takes about 1 minute to write that loop. Making it a function, as I did, a very little more time thinking about error handling and how to dress it up. About as much effort as writing a post of a few sentences here.

Villeroy had a solution ready made in a previous post - unfortunately not where it could be found with a search for "subset of an array". Now this thread is here anyone can do that search - and copy and paste from two ready made solutions - or see how easy it is to write it themselves, in about the same amount of time.

This forum has many posts with people asking how to do simple OO things in other languages from Basic and they're stuck - meanwhile there are a load of ready made basic examples and its easy peasy to do it in Basic. But they've chosen a different language and they're just stuck...
Last edited by JeJe on Fri May 12, 2023 8:34 am, edited 2 times in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Basic - subset of an array

Post by karolus »

DCTN54 wrote: Fri May 12, 2023 7:41 am
But, sorry, thats python, far beyond your capabilities!
Reading the subject is far beyond your capabilities it seems :shock: .
I asked about Basic because obviously in Python it is something very easy to do but not in Basic.
And why do you insist on BASIC when you know you could easily solve it with python?
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Basic - subset of an array

Post by karolus »

JeJe wrote: Fri May 12, 2023 8:27 am … and its easy peasy to do it in Basic.
If that were the case, the heroes wouldn't have to ask for it!
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: Basic - subset of an array

Post by JeJe »

My point was people ask to do simple things in other languages and they can't do it because they can't just look it up in some documentation and find an example as they can in Basic.

Its a better reason (for me anyway) to use Basic than Basic not having an inbuilt subarray function. I'd much rather have the Basic IDE than an inbuilt subarray function. If you know Basic you can write the function yourself in a minute....

But as I say... each to their own.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
karolus
Volunteer
Posts: 1158
Joined: Sat Jul 02, 2011 9:47 am

Re: Basic - subset of an array

Post by karolus »

Hallo
@JeJe:
I know BASIC well enough to solve the question in one or two minutes, but the point is that I am NOT stuck with BASIC or the Basic IDE!
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply