|
Post by bushrat on Jun 23, 2022 8:19:40 GMT -5
I do a lot of work with Arrays.
I often want to know if the value of some variable is a member of an array. I know it's easy to loop through the array to find if the value in question is one of the array elements, but this clutters up the code with repeated looping statements.
What would be really handy is if there was an array operator 'IN'.
I could then code as follows for value 'x' and array 'numbers':
if x IN numbers then <do something>
Does this make sense to anyone?
Thanks for your time
|
|
|
Post by tsh73 on Jun 23, 2022 9:05:47 GMT -5
While it would be nice to have build-in FAST operator for doing this we could use EVAL to make user-defined function. It even works
(have to run it a few times to actually caught something from b() array)
a() 5 6 6 5 6 7 0 is in array 0 5 is in array 1 3 is in array 0 4 is in array 0 2 is in array 0
------------------------------- b() 65 58 30 37 10 72 83 95 96 69 32 79 26 23 49 12 is in array 0 84 is in array 0 100 is in array 0 58 is in array 1 50 is in array 0 if array used from 1 then unused b(0) is 0, so it is in array 0 is in array 1
dim a(5) dim b(15)
for i = 0 to 5 ' index from 0 a(i)=int(rnd(0)*10)+1 'from 1 next
for i = 1 to 15 ' index from 1 b(i)=int(rnd(0)*100)+10 'from 10 next
print "a()" for i = 0 to 5 print a(i);" "; next print
for i = 1 to 5 value = int(rnd(0)*10) print value ;" is in array ";inArray("a", value) next print print "-------------------------------"
print "b()" for i = 1 to 15 print b(i);" "; next print
for i = 1 to 5 value = int(rnd(0)*100)+10 print value ;" is in array ";inArray("b", value) next
print "if array used from 1 then unused b(0) is 0, so it is in array" print 0 ;" is in array ";inArray("b", 0)
end
function inArray(arrName$, value) on error goto [err] inArray = 1 for i = 0 to 10000000000000000 ' we explicitely ask for out of array error, and then quit if eval(arrName$;"(";i;")")=value then exit function 'found, return 1 next [err] inArray=0 end function
|
|
|
Post by bushrat on Jun 23, 2022 9:25:22 GMT -5
Thank you for your suggestion.
I did contemplate using a user defined function as you suggest but decided it wouldn't be a whole lot better than keeping loops.
I'll look further into your function tomorrow seeing how it's 12:30 am my time.
Was hoping someone would 'magic-up' a FAST operator.
thanks again
|
|
|
Post by Rod on Jun 23, 2022 9:34:20 GMT -5
You might consider holding the data as a delimited string and using instr(
This would be the fastest way to find data
You might also hold the data in a sorted array, that also allows fast location of data.
|
|
|
Post by bushrat on Jun 23, 2022 20:24:33 GMT -5
I only use sorted arrays whenever possible.
I like the 'instr(' idea I'll give it a go.
But without a FAST array operator there are overheads no matter which way you want to go.
I guess that's about all I can say.
Thank you again for your suggestions.
|
|
|
Post by Brandon Parker on Jun 23, 2022 20:26:19 GMT -5
You might consider using the Dynamic Array library over on my forum. The Dynamic Array library is similar to what Anatoly (tsh73) showed above, but the project was started thirteen years ago and there are many different functions available. Import Architect Forum{:0) Brandon Parker
|
|
|
Post by Carl Gundel on Jun 24, 2022 9:27:01 GMT -5
What sort of array operators are common in other BASICs?
|
|
|
Post by bushrat on Jun 28, 2022 2:24:16 GMT -5
What sort of array operators are common in other BASICs? I've no idea, I was simply saying such a thing would be nice to have.
Also there are no statistical functions available in Liberty Basic, they also would be handy.
I'm not trying to denigrate Liberty Basic it's a great programming language which I've had a lot of fun with. But let's not get defensive just because it doesn't cover everything for the coder.
I thought maybe over time new stuff was introduced into the language?
Thanks for your reply.
|
|
|
Post by Rod on Jun 28, 2022 3:31:31 GMT -5
I think that was simply a genuine question from Carl. We currently have Sort(). It is fast and powerful. Sum() would be good, Average() too and probably more. Find() as you were asking for would be useful. We can program all of these functions ourselves but on large arrays it takes time.
But I don't know if the engine Liberty is built with can provide functions like these that would run as fast as Sort().
Now is the time to have the discussion so that LB5.1 might provide the tools.
|
|
|
Post by tsh73 on Jun 28, 2022 4:17:16 GMT -5
Really, common operation is to pass array into sub/function That means we need local arrays as well Without this, porting code from other BASICs involve creating elaborate workarounds
Also UBOUND() is pretty common function
|
|
|
Post by Carl Gundel on Jun 28, 2022 11:30:39 GMT -5
I'm not trying to denigrate Liberty Basic it's a great programming language which I've had a lot of fun with. But let's not get defensive just because it doesn't cover everything for the coder.
I thought maybe over time new stuff was introduced into the language? Hey, I was just curious what people think, so I asked.  As you say, over time new stuff gets introduced. Perhaps that will include more array operators.
|
|
|
Post by Rod on Jun 28, 2022 13:56:12 GMT -5
We don't need to break Liberty BASIC's global array model to get more out of arrays. We could benefit from local arrays and passing an array to a sub. But we could also gain from being able to say global array a()= global array(b).
So a()=b(), a()-b()=c() and t=sum(a(), a=average(a(), m=mean(a(), pos=find("x",a$(),pos), b()=abs(a()
Any other ideas? Once we have the ideas lined up we can ask if any are possible.
|
|
|
Post by Walt Decker on Jun 28, 2022 15:38:41 GMT -5
are matrix functions so let's add a() = CON(value), a() = IDeNtity, c() = a() * b()
|
|
|
Post by Carl Gundel on Jun 29, 2022 7:53:46 GMT -5
are matrix functions so let's add a() = CON(value), a() = IDeNtity, c() = a() * b() I don't understand this notation. Is this lifted from another BASIC language?
|
|
|
Post by Walt Decker on Jun 29, 2022 8:28:15 GMT -5
Yes.
A() = CON(Value) <--- place a constant value in all elements of A() A() = IDN <--- identity matrix (must be square, e.g 3 X 3, 4 X 4) C() = A() * B() <--- matrix multiplication
|
|