Page 1 of 1
[Solved] Array of unique values in StarBasic
Posted: Sat Mar 03, 2018 9:38 pm
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 ....
Re: Array of unique values ?
Posted: Sun Mar 04, 2018 1:06 am
by JeJe
You can use a Collection. See the later posts on this thread.
viewtopic.php?f=20&t=2953
Re: Array of unique values ?
Posted: Sun Mar 04, 2018 1:15 am
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
Re: Array of unique values ?
Posted: Sun Mar 04, 2018 11:33 am
by RPG
Re: Array of unique values ?
Posted: Sun Mar 04, 2018 1:50 pm
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...
Re: Array of unique values ?
Posted: Tue Sep 27, 2022 3:28 pm
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']