Results 1 to 6 of 6

Thread: Help assigning an array of strings to a set of variables

  1. #1
    iBot User
    Join Date
    Sep 2012
    Posts
    239
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    46 Post(s)
    Reputation
    23
    Rep Power
    5

    Check if string exists in table

    There's a table, e.g.:

    Code:
    {string1, string2, string3, string4, string5}
    How do I check if x string exists in table, returning true if it does, false otherwise?

    PS: "exists" here refers to being equal (==) to one of the strings in table. Containing part of the string is not enough.
    Last edited by john86; 07-13-2015 at 12:22 AM.

  2. #2
    iBot User
    Join Date
    May 2013
    Posts
    35
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    9 Post(s)
    Reputation
    10
    Rep Power
    4
    maybe you could do something like

    "string1" == "If cancast then cast..."

    then create a secondary script where the hud does something like "if cancast then hud = yes" or "if cancast then say "true string1" or something? not the best scripter but just tossing the idea if you get it.


    or even add withinthe script your working on "if string1 then ......"

  3. #3
    iBot User
    Join Date
    Sep 2012
    Posts
    239
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    46 Post(s)
    Reputation
    23
    Rep Power
    5
    So far I came up with this:

    Code:
    local strings = {"a","b","c"}
    
    if x == strings[1] or x == strings[2] or x == strings[3] then 
        //do something
    end
    But this scheme gets complicated when the table is large, so I'm still waiting a cleaner way to do it.

  4. #4
    Moderator Drakke's Avatar
    Join Date
    Jun 2012
    Location
    Sweden
    Posts
    1,898
    Mentioned
    216 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    390 Post(s)
    Reputation
    366
    Rep Power
    10
    @john86


    local strings = {'a', 'b', 'c'}

    for i=1,#strings do
    if x == strings[i] then
    //do something
    end
    end

  5. #5
    Administrator Tamapumper's Avatar
    Join Date
    Apr 2012
    Posts
    480
    Mentioned
    214 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    167 Post(s)
    Reputation
    177
    Rep Power
    10
    Quote Originally Posted by john86 View Post
    There's a table, e.g.:

    Code:
    {string1, string2, string3, string4, string5}
    How do I check if x string exists in table, returning true if it does, false otherwise?

    PS: "exists" here refers to being equal (==) to one of the strings in table. Containing part of the string is not enough.
    if you cannot want to use loops to do a search in your table, you can use the value as a key of the table, so just check if the key exists. (it will use less cpu)

    Example


    local tableData = {"string1" = true, "string2" = true, "string3" = true, "string4" = true, "string5" = true}

    if(tableData["string1"] ~= nil) then
    print("I EXIST!")
    end

  6. #6
    iBot User
    Join Date
    Sep 2012
    Posts
    239
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quoted
    46 Post(s)
    Reputation
    23
    Rep Power
    5
    @Drakke
    @Tamapumper

    Thanks.

    How can I randomly assign a table of four values, e.g.

    Code:
    local random_table = {"str1", "str2", "str3", "str4"}
    To a set of an equal number of variables (four), without repetition?

    Consider these four variables:

    Code:
    local var1, var2, var3, var4
    After randomly assigning the table values to the variables, it will look something like this (notice there's no repetition):

    Code:
    var1 = str3
    var2 = str4
    var3 = str1
    var4 = str2
    Last edited by john86; 07-13-2015 at 03:29 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •