[Solved] Basic - Subset of an array
[Solved] Basic - Subset of an array
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.
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
Re: Basic - subset of an array
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.
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.
Re: Basic - subset of an array
Hallo
But, sorry, thats python, far beyond your capabilities!
Code: Select all
…
new_data = data[10:30]
…
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Re: Basic - subset of an array
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.
Edit: added defining variables - always a good idea,
Edit: corrected error doing that!
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: corrected error doing that!
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Basic - subset of an array
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.
Push elements 3 to 9 of array a() into array b()
In Python b = a[3:9] writes elements 3 to 9 from array a to array b.
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
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
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Basic - subset of an array
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:
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)
Re: Basic - subset of an array
@Villeroy
Redim preserve is very expensive (Edit: in a loop). Here's a timing test.
(Python I presume would be much quicker.)
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)
Re: Basic - subset of an array
Reading the subject is far beyond your capabilities it seems .But, sorry, thats python, far beyond your capabilities!
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
Re: Basic - subset of an array
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
Re: Basic - subset of an array
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...
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)
Re: Basic - subset of an array
And why do you insist on BASIC when you know you could easily solve it with python?
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Re: Basic - subset of an array
If that were the case, the heroes wouldn't have to ask for it!
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Re: Basic - subset of an array
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.
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)
Re: Basic - subset of an array
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!
@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!
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)