So I'll jump right into it, I'm in a middle of making a program that right now requires me to substitute a variable based on if B=1...8 so that means 8 different variables and 8 if statements, right? I hope not. The basic premise is that I think there is a way to substitute variables like a For command without it calling the number instead of the symbol of that number because I need to store the output for the f(x) into the variable.

Example Code:

Code:
```For(B,1,8) If B=1:Str2->Str0 If B=2:Str3->Str0 If B=3:Str4->Str0 If B=4:Str5->Str0 If B=5:Str6->Str0 If B=6:Str7->Str0 If B=7:Str8->Str0 If B=8:Str9->Str0 End```

Potential Solutions:

Code:
```For(B,1,8) sub(Str2+Str3+Str4+Str5+Str6+Str7+Str8+Str9,B,length(StrB))->Str0 End```

Here is a potential solution that doesn't work because variables as far as I know are considered one character in TI-Basic and so inferring a number into a variable prefix doesn't make the prefix plus the inferred number one variable.

This could be easily solved if the strings were all made the same length using .... or [Spaces] at the end, but I'm writing a program to measure pixel value based on the strings contents so adding things at the end could lead to issues and bugs if that said symbol is included in the compared string.
The only real way to so this sanely is to use Ans to your advantage. For example, here's one way that could work:

Code:
```For(B,1,8) Str2 If B=2:Str3 If B=3:Str4 If B=4:Str5 If B=5:Str6 If B=6:Str7 If B=7:Str8 If B=8:Str9 Ans->Str0 End```

Of course, there are a couple other ways, but this may be the fastest and smallest in pure basic. Hope this helps to some degree!
Why is this in a for loop? It'll always finish the loop with the final case.
Because the for-if antipattern.
You could try using expr( to make the string of "Str" added to a string of the value of B. Maybe this?

Code:
```For(B,1,8) expr("Str"+sub("23456789",B,1))->Str0 End ```

I didn't test to see if this works, but I know that if you do expr("Str1")->Str0 works, so this might?
Haobo wrote:
You could try using expr( to make the string of "Str" added to a string of the value of B. Maybe this?
Unfortunately, this will not work. Str1 (for example) is a single token, not Str followed by a 1. expr("Str1") works because expr("Str1") is just the contents of Str1.
KermMartian wrote:
Haobo wrote:
You could try using expr( to make the string of "Str" added to a string of the value of B. Maybe this?
Unfortunately, this will not work. Str1 (for example) is a single token, not Str followed by a 1. expr("Str1") works because expr("Str1") is just the contents of Str1.

Would this work?

Code:
```For(B,1,8) expr(sub("Str2Str3Str4Str5Str6Str7Str8Str9",B,1))->Str0 End ```
Haobo wrote:
Would this work?

Code:
```For(B,1,8) expr(sub("Str2Str3Str4Str5Str6Str7Str8",B,1))->Str0 End ```
Yes, I believe that that would work; nice one!
Haobo wrote:
KermMartian wrote:
Haobo wrote:
You could try using expr( to make the string of "Str" added to a string of the value of B. Maybe this?
Unfortunately, this will not work. Str1 (for example) is a single token, not Str followed by a 1. expr("Str1") works because expr("Str1") is just the contents of Str1.

Would this work?

Code:
```For(B,1,8) expr(sub("Str2Str3Str4Str5Str6Str7Str8Str9",B,1))->Str0 End ```

YES! Thanks Haobo and Kerm that does work!

This shortens my code extremely, but to give an example here is a gif with some code in action.

Code:
```"Str2"->Str2 "Str3"->Str3 "Str4"->Str4 "Str5"->Str5 "Str6"->Str6 "Str7"->Str7 "Str8"->Str8 "Str9"->Str9 For(B,1,8) expr(sub("Str2Str3Str4Str5Str6Str7Str8Str9",B,1))->Str0 Disp Str0 End```

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.

»
» 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