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

Uh what is array

Root / Programming Questions / [.]

MZ952Created:
DEF F A$
 A$=G$()
 UNSHIFT A$,"D"
END
DEF G$()
 DIM T$[0]:RETURN T$
END
DIM A$[0]
PUSH A$,"ABC"
F A$?A$[0]

> ABC
rerun code with A$=G$() removed from F
...
> D
Setting A$ equal to G$() didn't set the value of A$ to the value of G$(), it made A$ reference G$(), which is a function not a variable so UNSHIFT A$,"D" was sent to the void. What I'm really trying to do is make it so that A$=G$() works. That is, clear the contents of A$ without having to POP every element away manually. Is this possible with maybe some clever manipulation of things?

Uh shoot I forgot to change the title before posting

Uh shoot I forgot to change the title before posting
LOL

A$ references the array returned by G$(), not the function G$().

I'm gonna shuffle things around a little to make your code clearer, so that it's easier to talk about what's going on.
DIM ARRAY$[0]
PUSH ARRAY$,"ABC"
F ARRAY$
?ARRAY$[0] 'ABC

DEF EMPTY$()
 DIM OUT$[0]
 RETURN OUT$
END

DEF F ARG$
 ARG$=EMPTY$()
 UNSHIFT ARG$,"D"
END
What you're expecting to happen is that F will modify ARRAY$ to be the array ["D"], but instead it's not modified at all. It's important to understand the difference between variables and values here. The variables ARRAY$ and ARG$ each contain a reference to the same array ["ABC"], but when you run ARG$=EMPTY$(), you are changing the variable ARG$, making it hold a new reference to an empty array. ARRAY$ still has the reference to the old array, so it's not affected. This is different from actually modifying the array the reference points to, in which case you'd see the change in both ARRAY$ and ARG$.

A$ references the array returned by G$(), not the function G$().
That's kinda what I meant If only I could do that one trick that makes it not reference type. Like
F(_S$)
DIM S$=_S$
INC S$,"D"
RETURN S$
END
so that
?F("ABC")
doesn't fail. We can't perform a reference type operation on a string literal, so we create a variable to operate on. That, except with string arrays. After setting A$ to reference G$() (or the return of G$() like you said), we can't seem to do anything to A$ until the reference is broken by something. For strings, that's simply something like S$=S$+"". Just need a string array equivalent I guess. Maybe SB4's copy function could be used for this, which can take parenthetical form now. Too bad I'm stuck on SB3.

... but when you run ARG$=EMPTY$(), you are changing the variable ARG$, making it hold a new reference to an empty array. ARRAY$ still has the reference to the old array, so it's not affected.
So what I should do is directly reference the variable with VAR(...)
DEF F VARNAME$
 VAR(VARNAME$)=EMPTY$()
 UNSHIFT VAR(VARNAME$),"D"
END
DEF EMPTY$()
 DIM TMP$[0]:RETURN TMP$[0]
END

So what I should do is directly reference the variable with VAR(...)
That is one way to do it, but if you're clearing it in the first place, I'd probably just create a new array and return it, like this:
DIM ARRAY$[0]
PUSH ARRAY$,"ABC"
ARRAY$=F$()
?ARRAY$[0] 'D

DEF EMPTY$()
 DIM OUT$[0]
 RETURN OUT$
END

DEF F$()
 DIM OUT$=EMPTY$()
 UNSHIFT OUT$,"D"
 RETURN OUT$
END

Yeah, ideally. Problem is that in my specific case F can't be parenthetical like that It's good to learn this though. I was mystified for a bit there, but that statement I quoted really cleared it up for me, thanks

I usually do that like this:
DIM ARRAY$[0]
PUSH ARRAY$,"ABC"
F ARRAY$
?ARRAY$[0]

DEF ARYCLR ARG$
 VAR N$
 WHILE LEN(ARG$)
  N$=POP(ARG$)
 WEND
END

DEF F ARG$
 ARYCLR ARG$
 UNSHIFT ARG$,"D"
END

Yeah, but that's exactly what I'd like to avoid. For large arrays, popping everything off the stack can take a while Also, I think this may be a better approach
DEF CLR A[]
 WHILE LEN(A) IF POP(A) THEN ENDIF WEND
END
because stowing into a variable also takes up precious milliseconds.

I tested a while ago and iirc using a temporary variable is the fastest

I tested a while ago and iirc using a temporary variable is the fastest
No way