2

Is it possible to to create a program on the TI-84 that contains a large array and get data from that array? I will have the program prompt for something and I'd like it to see if it can find the prompt entered in an array.

For example, let's say this is the array:

array("SEARCH1" => "ANSWER1", "SEARCH2" => "ANSWER2")

When I input SEARCH1 I'd like the calculator to return ANSWER1.

lirtosiast
  • 592
  • 4
  • 15
Matt Kelly
  • 31
  • 4
  • Welcome to Stack Overflow! Please post a [MCVE](https://stackoverflow.com/help/mcve); you are more likely to get help here if you let us know specifics about what you've already tried, and what you are actually trying to do. – dho Mar 12 '16 at 00:19
  • I don't understand exactly. Are you trying to implement a dictionary data structure, where keys are strings and values are strings? – lirtosiast Mar 12 '16 at 00:49
  • @lirtosiast Yes, exactly. Is that possible? – Matt Kelly Mar 12 '16 at 00:53
  • There is no builtin dictionary data structure. I'll write an answer with a possible implementation. – lirtosiast Mar 12 '16 at 00:56
  • @lirtosiast okay, thank you. I appreciate it! – Matt Kelly Mar 12 '16 at 01:18
  • 1
    This is very much a *please give me teh codez* question. Have you made any effort to do *anything* yourself first? Questions asking *is this possible?* don't fare well here; possible answers are *Yes, it's possible*, *No, it's not possible*, or *Maybe, depending.*. Which one of those can I post for you that will be useful to you? If the answer is *None*, your question is poorly phrased and you need to edit it to make it more specific. If the answer is other than *None*, let me know which one works, I'll post it as an answer, and you can accept it as correct. :-) – Ken White Mar 12 '16 at 01:50

1 Answers1

3

TI-BASIC doesn't have dictionaries

There are no lists/arrays of strings either. However, it's possible to implement one using strings. We'll use Str1 for the large string that contains all keys and values. Use a delimiter (say ?) to start keys, and another one (say !) to start values. You can represent the list thusly:

//starting delimiter
"?->Str1

//add value "SEARCH1" => "ANSWER1" at end
Str1+"SEARCH1!ANSWER1?→Str1

//add second value
Str1+"SEARCH2!ANSWER2?→Str1

Str1 is now ?SEARCH1!ANSWER1?SEARCH2!ANSWER2?.

Then to access the value corresponding to the key Str0=SEARCH1:

"SEARCH1→Str0
inString(Str1,"?"+Str0+"!")+length(Str0)+2   //now Ans = index of key
sub(Str1,Ans,inString(Str1,"?",Ans)-Ans      //get the key

The performance of this can be slightly improved through tricks. However, as Str1 gets larger, this routine gets slower—it does a linear search, O(n) through the whole string to find the key. If you want O(1) access, implementation will be significantly more complicated, as it requires hashing.

Community
  • 1
  • 1
lirtosiast
  • 592
  • 4
  • 15
  • The code works flawlessly, but is it possible for me to type Str0 through Prompt? I've tried simply using "Prompt Str0" but that returns an error – Matt Mar 12 '16 at 03:19
  • 2
    @Matt `Prompt Str0` wants something that evaluates to a string, e.g. `"SEARCH1"`, so you don't get a data type error. To input without the quotes, use `Input Str0` instead. – lirtosiast Mar 12 '16 at 04:45