Announcement

Collapse
No announcement yet.

Table (Array) - get results through variable Selection?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Table (Array) - get results through variable Selection?

    Hi all,

    Im trying to build a table of array and i want to get the result through a variable by an input.

    I thought its simple and i built it as i understood the manual , jut put a variable inside the [] and it will work but it didn't work for me

    I have 2 Inputs , one to put selection and the second to show the selection data inside the table.

    This is the code in a button:

    Code:
    My_Table = {};
    My_Table[1] = "Selection 1";
    My_Table[2] = "Selection 2";
    My_Table[3] = "Selection 3";
    My_Table[4] = "Selection 4";
    My_Table[5] = "Selection 5";
    
    MySelection = Input.GetText("Input1");
    
    result = Input.SetText("Input2", My_Table[MySelection]);
    Hope you guys give me the right way to Read and Write to a specific item in the Table through a variable.



  • #2
    It's unclear on exactly what your end objective is here. The partial code sample you've posted doesn't really make sense in its current form. From the limited info in your request, the best I can guess/infer is that you want to output the value of an item from an array, according to user input expressed as an index-number from the array.

    For this to work in the format you've implied, user input would have to be limited to any number from 1-5 (given that you have expressed an array comprised of 5 values). And could be coded this way:

    Code:
    My_Table = {};
    My_Table[1] = "Selection 1";
    My_Table[2] = "Selection 2";
    My_Table[3] = "Selection 3";
    My_Table[4] = "Selection 4";
    My_Table[5] = "Selection 5";
    
    Input.SetText("Input2", "");
    MySelection = Input.GetText("Input1");
    
    if string.match(MySelection, '[1-5]') then
    
    for k,v in pairs (My_Table) do
        k = tostring(k);
        if k == MySelection then
            Input.SetText("Input2", v);
        end
    end
    
    else
        Dialog.Message("Notice", "Input must be a number from 1-5");
    end
    Obviously, the code would need to be altered, if there are changes to the number of items in the array. Or if you wish to detect user input via a different format. (Commented example attached).
    Attached Files

    Comment


    • #3
      Edit,
      For further example regarding accessing of values from an array, see here: https://forums.indigorose.com/forum/...299#post300299

      Comment


      • #4
        As usual the first who jump to help , Thank you BioHazard , the result of your Code is exactly what i need, but i needed it to be more simple, so i learned from your code what i was missing,
        and i came up with this code and it worked

        Code:
        My_Table = {};
        My_Table[1] = "Selection 1";
        My_Table[2] = "Selection 2";
        My_Table[3] = "Selection 3";
        My_Table[4] = "Selection 4";
        My_Table[5] = "Selection 5";
        
        MySelection = Input.GetText("Input1");
        t = String.ToNumber(MySelection);
        Input.SetText("Input2", My_Table[t]);
        what i needed is to convert the string input to a number and it worked.

        Thank you again for you help.

        Comment


        • #5
          Originally posted by sameer valva View Post
          ... i needed it to be more simple ...
          You'll need to code for user input not equaling 1-5. Otherwise as soon as the user enters something different (or leaves input blank), you'll get an error-message being returned for Arg2 in the Imput.SetText() command. Hence the reason for the more complex code originally provided.


          ... what i needed is to convert the string input to a number and it worked ...
          Just an FYI for your future reference, the String.ToNumber() command is an AMS specific command. And can be substituted with the more compact, pure Lua equivalent of tonumber().

          Eg.
          Code:
          t = String.ToNumber(MySelection);
              [COLOR=#008000][I]-- can be replaced with[/I][/COLOR]
          t = tonumber(MySelection);

          Comment


          • #6
            Originally posted by BioHazard View Post
            You'll need to code for user input not equaling 1-5. Otherwise as soon as the user enters something different (or leaves input blank), you'll get an error-message being returned for Arg2 in the Imput.SetText() command. Hence the reason for the more complex code originally provided.



            Just an FYI for your future reference, the String.ToNumber() command is an AMS specific command. And can be substituted with the more compact, pure Lua equivalent of tonumber().

            Eg.
            Code:
            t = String.ToNumber(MySelection);
            [COLOR=#008000][I]-- can be replaced with[/I][/COLOR]
            t = tonumber(MySelection);

            I meant complex for using ( for k,v in pairs .....). I just wanted the Code to be more simple.

            The input is limited by another way , user will select a button that will give the output from the table, and i don't know how many buttons we will need, so i wanted something that will work whenever i add a new button (when we add a new value in the table). So i used an input in the example because i don't know the final number of entries but i needed to learn how it works.

            Code:
             
             t = String.ToNumber(MySelection);     [COLOR=#008000][I]-- can be replaced with[/I][/COLOR] t = tonumber(MySelection);
            Now this is the way i like to learn ,how to be done with the least number of lines and characters in the Code. I don't mind if we are forced to write long and Complex Codes if its the only and the better way, but if thing can be simplified , Why not ?

            Again thank you for not only answering the question, but for your explanation which is the very important thing, learning how the solution is made and how or why it works is what we need, and you are always generous for giving answers and explanations.

            Comment


            • #7
              You're most welcome!

              Comment

              Working...
              X