[Solved] Array of unique values in StarBasic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
BubikolRamios
Posts: 91
Joined: Sat Jan 04, 2014 1:28 pm

[Solved] Array of unique values in StarBasic

Post by BubikolRamios »

Is there predefined object in OO basic that is defined as array of unique values, so that, like :
array.add ("a") --> "a" added
array.add ("a") --> nothing added

I expect array will be huge, rolling thru it to check each time adding, if elemet is already in, would be a pain ....

 Edit: Changed subject, was Array of unique values ? 
Make your post understandable by others 
-- MrProgrammer, forum moderator 
Last edited by MrProgrammer on Mon Aug 28, 2023 4:56 am, edited 1 time in total.
Reason: Tagged ✓ [Solved] -- MrProgrammer, forum moderator
OPen office 4.1.5/ win 7
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Array of unique values ?

Post by JeJe »

You can use a Collection. See the later posts on this thread.

viewtopic.php?f=20&t=2953
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Array of unique values ?

Post by JeJe »

To check if an item with a given key is already in the collection you need an error trap.
Some versions of this to try are here (VB6 is similar to OOBasic).

https://stackoverflow.com/questions/406 ... collection
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Array of unique values ?

Post by RPG »

viewtopic.php?f=20&t=61325
Can a user instantiate a built-in object? (com.sun.star.*) (View topic) • Apache OpenOffice Community Forum
The links has also information.
But maybe more easy is: use a dBase(dbf) table with a unique index.

Romke
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Array of unique values ?

Post by JeJe »

You can also keep the array sorted or else keep a sorted index into the array - a binary search algorithm is a very efficient way of finding something in a sorted list. You don't need to reinvent the wheel: search for VB6 (similar to OO and a much bigger code base out there) along with your search term - you might need to do a little adapting. Here are some sorting algorithms:

http://www.vbforums.com/showthread.php? ... ng-arrays)

But I'd use a collection... they're very simple to use...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
wayne1426
Banned
Posts: 4
Joined: Thu Jan 22, 2009 6:54 pm

Re: Array of unique values ?

Post by wayne1426 »

BubikolRamios wrote: Sat Mar 03, 2018 9:38 pm Is there predefined object in OO basic that is defined as array of unique values, so that, like :
array.add ("a") --> "a" added
array.add ("a") --> nothing added

I expect array will be huge, rolling thru it to check each time adding, if elemet is already in, would be a pain ....
some require writing code, some use simple arrays and some use methods provided by the JavaScript language. A unique value in an array JavaScript is a value that appears exactly once. If you have an array with three integers, a unique value would be one of those three integers.
javascript find unique values in the array

Code: Select all

// usage example:
var myArray = ['a', 1, 'a', 2, '1'];
var unique = myArray.filter((v, i, a) => a.indexOf(v) === i); 

// unique is ['a', 1, 2, '1']
OOo 2.3.X on Ms Windows XP
Post Reply