Search code examples
arraysti-basic

Creating an array and grabbing data on TI-84


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.


Solution

  • 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.