LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

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?

As far as I'm aware, the only time you're allowed to refer to an array by its bare name alone is when you're passing it as an argument to something, or assignment as you stated above. No, I don't think this can be done, sorry

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.
ARRAYMATCH=TRUE
FOR I=0 TO 99.   '99 being the last slot
 IF ARRAY1[I]!=ARRAY2[I] THEN
  ARRAYMATCH=FALSE
  BREAK
 ENDIF
NEXT

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.
ARRAYMATCH=TRUE
FOR I=0 TO 99.   '99 being the last slot
 IF ARRAY1[I]!=ARRAY2[I] THEN
  ARRAYMATCH=FALSE
  BREAK
 ENDIF
NEXT
That checks if the two arrays hold equal values, not if they are pointing to the same address.

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.
DEF ISALIAS(ARRAY1,ARRAY2)
PUSH ARRAY1,LEN(ARRAY1)==LEN(ARRAY2)
VAR MATCH=LEN(ARRAY1)==LEN(ARRAY2)
RETURN POP(ARRAY1) && MATCH
END
Ah, that's brilliant! Thanks!

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
Or arrays with more than one dimension. Very slick code, though. Nice one, calc84maniac.

Very slick indeed!

is MATCH really nessesary?
DEF ISALIAS(ARRAY1,ARRAY2)
PUSH ARRAY1,LEN(ARRAY1)==LEN(ARRAY2)
RETURN POP(ARRAY1) && LEN(ARRAY1)==LEN(ARRAY2)
END
and 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:
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?
can you please explain what a pionter is?

is MATCH really nessesary?
DEF ISALIAS(ARRAY1,ARRAY2)
PUSH ARRAY1,LEN(ARRAY1)==LEN(ARRAY2)
RETURN POP(ARRAY1) && LEN(ARRAY1)==LEN(ARRAY2)
END
and 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)
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.

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 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?
can you please explain what a pionter is?
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:
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?
can you please explain what a pionter is?
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.
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.

Oh, wow I didn't know you could do ARRAY1=ARRAY2 I thought you could only make 2 arrays point to the same address using functions:
DIM ARRAY1[1]
ARRAY1[0]=12
EXAMPLE ARRAY1
?ARRAY1[0]

DEF EXAMPLE ARRAY2
 ARRAY2[0]=21
END
prints 21

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.

yeah, even smileboom pretends to not know about functions returning arrays there's no array=LOAD(FILE$,0), for example

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
SB evaluates right-to-left, so this won't work This one works:
DEF ISALIAS(ARRAY1,ARRAY2)
 PUSH ARRAY1,LEN(ARRAY2)
 RETURN -POP(ARRAY1)+LEN(ARRAY2)
END

SB evaluates right-to-left
Interesting 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