Hi, i haven't been around in a while, but I came back here to ask about substrings.
Currently making a remake on the CE of the web game Loop Over (at loopover[dot]xyz) largely to remedy my own boredom. however i have reached an issue where I am trying to store a character from a string at position E into that same string at position D, and so on, sort of cycling certain characters within the string. The code looks like this, with ... representing irrelevant code in-between and // representing comments:

Code:

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"->Str1
...
:If K=41     //if math key is pressed
:Then
:1->D
:6->E
:11->F
:16->G
:21->H
:End
:If not(S) and K≥41      //if direction is up+down and the key pressed is not an arrow key
:Then
:sub(Str1,D,1)->sub(Str1,26,1      //errors here
:sub(Str1,E,1)->sub(Str1,D,1
:sub(Str1,F,1)->sub(Str1,E,1
:sub(Str1,G,1)->sub(Str1,F,1
:sub(Str1,H,1)->sub(Str1,G,1
:sub(Str1,26,1)->sub(Str1,H,1
...


There are no comments in the actual code, so that is not the cause of the error.
Anyone have any ideas to do what I am trying to do?
StrawberryFrostedPopTart wrote:
Anyone have any ideas to do what I am trying to do?

You can't actually change one character of a string like that in TI-Basic, you'd have to split up the string and join it up again, here's an example:
instead of this:

Code:
sub(Str1,D,1)->sub(Str1,26,1

It would look something like this:

Code:
sub(Str1,27,length(Str1)-26
sub(Str1,1,25)+sub(Str1,D,1)+Ans

Although if you're gonna be doing this a lot of times, I'd suggest encoding your string into a list and manipulating that, which would be a lot faster and probably smaller
You can't actually store into a substring. sub() returns a immutable TI string, rather than a reference to a string.

You can do the same thing by concatenating strings - for example, to replace the 3rd character in Str1 with Str2, do sub(Str1,1,2)+Str2+sub(Str1,4,length(Str1)-3)->Str1
Unfortunately, you can’t store into substrings like you could if it was a char array. Strings in ti-basic don’t work like that. However, you can restate what your string is and concatenate the substrings you want to make your new string, like mr womp womp showed. Should be pretty straightforward, let us know if you need more help!
mr womp womp wrote:
StrawberryFrostedPopTart wrote:
Anyone have any ideas to do what I am trying to do?

You can't actually change one character of a string like that in TI-Basic, you'd have to split up the string and join it up again, here's an example:
instead of this:

Code:
sub(Str1,D,1)->sub(Str1,26,1

It would look something like this:

Code:
sub(Str1,27,length(Str1)-26
sub(Str1,1,25)+sub(Str1,D,1)+Ans

Although if you're gonna be doing this a lot of times, I'd suggest encoding your string into a list and manipulating that, which would be a lot faster and probably smaller

Thank you, this is the perfect solution Very Happy

Edit, still running into a few issues with this code. I replaced all of the sub( commands above with mr womp womp's solution.
The code is here: https://pastebin.com/EL4uJDL1
I hope it's legible. I run into another issue here, now having ERR:DOMAIN thrown at me every time i run the program, domain taking me to the third sub( command. I assume this has to do with D-1 being 0, which is invalid.
Additionally, if I replace D-1 with somehting valid (such as 1), no error is returned anymore, however literally nothing happens upon pressing Math, Apps, Prgm, Vars, or Clear, each of which should cycle up their respective row when S=0. I have not implemented funcitonality for cycling the rows down or left/right, however I feel that those will have similar issues if implemented.
https://hastebin.com/zumabukili.php This should be the full program, minus a timer and hiscores along the side which I plan to implement in the future. It's based off of [url]loopover.xyz[/url], for a reference of what it's supposed to do.
Hitting up or down on the arrow keys will set the board's columns to be moving up and down. Then, math,apps,prgm,vars,clear will move their respective columns up, and x^-1,sin,cos,tan,^ will move their respective columns down.
Hitting left or right will set the board's rows to be moving left and right. Then math,x^-1,x^2,log,ln will move their respective rows left, and apps,sin,[,],[7],[4] will move their respective rows right. The controls need work and are tentative, but those can be changed rather easily.

However none of this works. hitting the arrow keys will select the direction, but hitting any of the keys to loop the rows of letters does nothing, and hitting certain ones (namely math,x^-1) will spit a ERR:DOMAIN at you. i have been messing with this and i have literally no clue how to fix it.

edit:clarification and grammar
Ok so I tried your code out, it looks like you've got a good start but needless to say it needs some work.

(I'm going a bit easy on you here though, you should definitely look into eliminating labels and learning for loops and plenty of other optimizations once you've learned more coding)

So after a first glance here are the first 2 things I see-

Code:
sub(Str1,27,length(Str1)-26
sub(Str1,1,25)+sub(Str1,D,1)+Ans
sub(Str1,D+1,length(Str1)-D
sub(Str1,1,D-1)+sub(Str1,E,1)+Ans
sub(Str1,E+1,length(Str1)-E
sub(Str1,1,E-1)+sub(Str1,F,1)+Ans
sub(Str1,F+1,length(Str1)-F
sub(Str1,1,F-1)+sub(Str1,G,1)+Ans
sub(Str1,G+1,length(Str1)-G
sub(Str1,1,G-1)+sub(Str1,H,1)+Ans
sub(Str1,H+1,length(Str1)-H
sub(Str1,1,H-1)+sub(Str1,26,1)+Ans

This does nothing but put String values in Ans and overwrite Ans 12 times over. I'd say you just need to make sure you're actually storing your values into Str1, but you also have Domain errors here like you said so let's take a look at that.


Code:
sub(Str1,27,length(Str1)-26

So I know part of mr womp womp's suggestion was this, but actually since Str1 is only 26 characters long this line is completely unnecessary. There's nothing to add after index 26, so this line can be deleted.


Code:
sub(Str1,1,25)+sub(Str1,D,1)+Ans

Since we deleted the line before this one, we can also delete the ")+Ans". What you need to do now is make sure everything is storing to Str1, so the final code for this section would be:


Code:
sub(Str1,1,25)+sub(Str1,D,1->Str1
sub(Str1,D+1,length(Str1)-D
sub(Str1,1,D-(D>1))+sub(Str1,E,1)+Ans->Str1
sub(Str1,E+1,length(Str1)-E
sub(Str1,1,E-(E>1))+sub(Str1,F,1)+Ans->Str1
sub(Str1,F+1,length(Str1)-F
sub(Str1,1,F-(F>1))+sub(Str1,G,1)+Ans->Str1
sub(Str1,G+1,length(Str1)-G
sub(Str1,1,G-(G>1))+sub(Str1,H,1)+Ans->Str1
sub(Str1,H+1,length(Str1)-H
sub(Str1,1,H-(H>1))+sub(Str1,26,1)+Ans->Str1

Note, I also replaced the "D-1", "E-1" etc with "D-(D>1)", "E-(E>1)" etc to avoid the other domain errors.

Anyways the program successfully rotates the characters up and down now! Shouldn't be any errors, but let me know if any other issues arise that you can't fix.

EDIT: just so you know, wherever you use "Goto K" in your code, you've got a memory leak. You can usually fix this and avoid using labels and gotos altogether if you use loops properly.

EDIT 2: I also would suggest making your controls a bit easier to understand, if you want maybe you could use row and column pointers on the board instead?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement