? Uh what is array (Page 1) ● SmileBASIC Source Forums

Sign In

Register
*Usernames are case-sensitive
Forgot my password
What's going on?? See: Site Update March 2022

Uh what is array

  • #1 ✎ 412 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies 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? Posted
  • #2 ✎ 412 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies Uh shoot I forgot to change the title before posting Posted
  • #3 ✎ 117 Midnight Deep Sleep Hidden Website
    Uh shoot I forgot to change the title before posting
    LOL
    Posted
  • #4 ✎ 1131 snail_ Power User QSP Contest 1 Contest Participant I participated in the first SmileBASIC Source QSP Contest! Helper Received for being very helpful around SmileBASIC Source Achievements Amazing Contributor Someone thinks I'm an awesome person who has done so much for the community! Achievements A$ references the array returned by G$(), not the function G$(). Posted
  • #5 ✎ 255 niconii Power User Video Games I like to play video games! Hobbies Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies 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$. Posted Edited by niconii
  • #6 ✎ 412 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies
    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.
    Posted
  • #7 ✎ 412 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies
    ... 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
    Posted
  • #8 ✎ 255 niconii Power User Video Games I like to play video games! Hobbies Expert Programmer Programming no longer gives me any trouble. Come to me for help, if you like! Programming Strength Drawing I like to draw! Hobbies
    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
    Posted
  • #9 ✎ 412 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies 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 Posted
  • #10 ✎ 18 Na_trium 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 Posted
  • #11 ✎ 412 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies 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. Posted
  • #12 ✎ 1884 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself I tested a while ago and iirc using a temporary variable is the fastest Posted
  • #13 ✎ 412 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Third Year My account is over 3 years old Website Reading I like to read books! Hobbies
    I tested a while ago and iirc using a temporary variable is the fastest
    No way
    Posted