Can you check if two arrays are the same in SmileBASIC?
Root / Programming Questions / [.]
amihartCreated:
I know arrays are pointers, so that if you say something like:
DIM ARRAY1[1] DIM ARRAY2[1] ARRAY2 = ARRAY1 ARRAY2[0]=5 PRINT ARRAY1[0]Most newbies might think you'd get "0" as an output, but you get 5. Because, at least as I understand it, arrays are pointers, so when you assign one array to another, it just makes them store the same pointer address rather than copying the actual values, so editing one affects both. This is the same as it is in Java, however, in Java, you can compare to arrays using "ARRAY1==ARRAY2" and it will return true if they are pointing to the same address. There are quite a few times you can make use of this. However, in SmileBASIC, when I call "ARRAY1==ARRAY2", I simply get a "Type mismatch" error. I'm wondering if there's a way to check if two arrays are pointing to the same address. I don't want to see if they are holding the same values, but if they are pointing to the same address. Is there a way I can do this in SmileBASIC?
You would need to write a function to do it, a FOR loop could easily manage it. This is a crude example, of note it would be slower the larger the array.That checks if the two arrays hold equal values, not if they are pointing to the same address.ARRAYMATCH=TRUE FOR I=0 TO 99. '99 being the last slot IF ARRAY1[I]!=ARRAY2[I] THEN ARRAYMATCH=FALSE BREAK ENDIF NEXT
This function should work, as long as you're not using string arrays.
DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1,LEN(ARRAY1)==LEN(ARRAY2) VAR MATCH=LEN(ARRAY1)==LEN(ARRAY2) RETURN POP(ARRAY1) && MATCH END
This function should work, as long as you're not using string arrays.Or arrays with more than one dimension. Very slick code, though. Nice one, calc84maniac.DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1,LEN(ARRAY1)==LEN(ARRAY2) VAR MATCH=LEN(ARRAY1)==LEN(ARRAY2) RETURN POP(ARRAY1) && MATCH END
is MATCH really nessesary?
DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1,LEN(ARRAY1)==LEN(ARRAY2) RETURN POP(ARRAY1) && LEN(ARRAY1)==LEN(ARRAY2) ENDand for string arrays:
DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1," "*(LEN(ARRAY1)==LEN(ARRAY2)) RETURN LEN(POP(ARRAY1)) && LEN(ARRAY1)==LEN(ARRAY2) END(maybe there's an even better way)
I know arrays are pointers, so that if you say something like:can you please explain what a pionter is?DIM ARRAY1[1] DIM ARRAY2[1] ARRAY2 = ARRAY1 ARRAY2[0]=5 PRINT ARRAY1[0]Most newbies might think you'd get "0" as an output, but you get 5. Because, at least as I understand it, arrays are pointers, so when you assign one array to another, it just makes them store the same pointer address rather than copying the actual values, so editing one affects both. This is the same as it is in Java, however, in Java, you can compare to arrays using "ARRAY1==ARRAY2" and it will return true if they are pointing to the same address. There are quite a few times you can make use of this. However, in SmileBASIC, when I call "ARRAY1==ARRAY2", I simply get a "Type mismatch" error. I'm wondering if there's a way to check if two arrays are pointing to the same address. I don't want to see if they are holding the same values, but if they are pointing to the same address. Is there a way I can do this in SmileBASIC?
is MATCH really nessesary?These will return TRUE if the lengths of the arrays are equal at the time the function is called. MATCH is to check that the lengths of the arrays are also equal during that time after the PUSH and before the POP. Nice solution for strings.DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1,LEN(ARRAY1)==LEN(ARRAY2) RETURN POP(ARRAY1) && LEN(ARRAY1)==LEN(ARRAY2) ENDand for string arrays:DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1," "*(LEN(ARRAY1)==LEN(ARRAY2)) RETURN LEN(POP(ARRAY1)) && LEN(ARRAY1)==LEN(ARRAY2) END(maybe there's an even better way)
I haven't tested it, but assuming left-to-right order of evaluation, this might also work:
DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1,LEN(ARRAY2) RETURN LEN(ARRAY2)-POP(ARRAY1) END
I don't think a pointer is really correct, I think he just means are both variables referring to the same array. From my understanding BASIC doesn't work like that, unlike Java which is OOP.I know arrays are pointers, so that if you say something like:can you please explain what a pionter is?DIM ARRAY1[1] DIM ARRAY2[1] ARRAY2 = ARRAY1 ARRAY2[0]=5 PRINT ARRAY1[0]Most newbies might think you'd get "0" as an output, but you get 5. Because, at least as I understand it, arrays are pointers, so when you assign one array to another, it just makes them store the same pointer address rather than copying the actual values, so editing one affects both. This is the same as it is in Java, however, in Java, you can compare to arrays using "ARRAY1==ARRAY2" and it will return true if they are pointing to the same address. There are quite a few times you can make use of this. However, in SmileBASIC, when I call "ARRAY1==ARRAY2", I simply get a "Type mismatch" error. I'm wondering if there's a way to check if two arrays are pointing to the same address. I don't want to see if they are holding the same values, but if they are pointing to the same address. Is there a way I can do this in SmileBASIC?
It appears to act like a pointer, though. :/ Arrays in Java and C++ are pointers and behave the same way. That's why if you set ARRAY2=ARRAY1 and you modify ARRAY2, ARRAY1 gets modified as well, because they are simply pointers to the memory location and not primitive variables in themselves. That's also why you can simply set ARRAY1=ARRAY2 no matter what the size is of ARRAY1, because you're simply changing the memory it points to, the "size" attribute isn't part of the pointer itself. The OOP doesn't really have anything to do with it, as arrays act exactly the same in C as well, which isn't OOP. At least, this is how I understand it, if anyone wants to correct me on anything.I don't think a pointer is really correct, I think he just means are both variables referring to the same array. From my understanding BASIC doesn't work like that, unlike Java which is OOP.I know arrays are pointers, so that if you say something like:can you please explain what a pionter is?DIM ARRAY1[1] DIM ARRAY2[1] ARRAY2 = ARRAY1 ARRAY2[0]=5 PRINT ARRAY1[0]Most newbies might think you'd get "0" as an output, but you get 5. Because, at least as I understand it, arrays are pointers, so when you assign one array to another, it just makes them store the same pointer address rather than copying the actual values, so editing one affects both. This is the same as it is in Java, however, in Java, you can compare to arrays using "ARRAY1==ARRAY2" and it will return true if they are pointing to the same address. There are quite a few times you can make use of this. However, in SmileBASIC, when I call "ARRAY1==ARRAY2", I simply get a "Type mismatch" error. I'm wondering if there's a way to check if two arrays are pointing to the same address. I don't want to see if they are holding the same values, but if they are pointing to the same address. Is there a way I can do this in SmileBASIC?
It's also commonly believed you can't return arrays from functions. You can.
DEF THEFIRSTFIVENUMBERS() DIM A%[5] VAR I% FOR I%=0 TO 4 A%[I%]=I%+1 NEXT RETURN A% END 'Use as value PRINT THEFIRSTFIVENUMBERS()[2] 'Use as assignment DIM NUMS%[0] NUMS%=THEFIRSTFIVENUMBERS()My assumption here is that it returns a reference to an anonymous array in the heap (hence, all arrays are in the heap) but instead of immediately collecting it when it exits scope it deals with it within the current statement as it should.
I haven't tested it, but assuming left-to-right order of evaluation, this might also work:SB evaluates right-to-left, so this won't work This one works:DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1,LEN(ARRAY2) RETURN LEN(ARRAY2)-POP(ARRAY1) END
DEF ISALIAS(ARRAY1,ARRAY2) PUSH ARRAY1,LEN(ARRAY2) RETURN -POP(ARRAY1)+LEN(ARRAY2) END
SB evaluates right-to-leftInteresting consequence:
A$="ABC" OK PRINT SHIFT(A$) A OK ' That's what I expected OK PRINT SHIFT(A$)+SHIFT(A$) CB OK ' This is a bit of a surprise. OK