title: How can I search one table for a value from another table?
body: <p>This is for a simple script I'm writing for an oldschool MUD game for those who know what that is.</p>
  
  <p>Basically I am searching through one table (which I'm reading from gmcp) and trying to search the value's on that table to see if any of them match any of the value's on another table which I'm storing the value's I'm looking for. </p>
  
  <p>I've successfully managed to do it with singular value's by simply using a "for" loop to grab the value from gmcp and store it as a variable and then using another "for" loop to search the other table to see if any of the values there match the variable.</p>
  
  <p>Trouble is, it only works for a singular value and misses all the others if there is more than one value I need to check in that table.</p>
  
  <p>The code I have is as follows,</p>
  
  <pre><code>for _, v in pairs(gmcptable) do
    checkvalue = v
  end
  for _, v in pairs(mytable) do
    if v == checkvalue then
      echo("yay")
    else
      echo("no!")
    end
  end
  </code></pre>
  
  <p>again this works fine for gmcp tables with one value, but fails if more. I tried doing this to,</p>
  
  <pre><code>for _, v in pairs(gmcptable) do
    checkvalue = v
    for _, v in pairs(mytable) do
      if v == checkvalue then
        echo("yay")
      else
        echo("no!")
      end
    end
  end
  </code></pre>
  
  <p>my hope was that it might set the variable, run the second for loop to check the variable and then repeat for the next value on the gmcp table since it's a for loop and the second loop was within the loop, but that didn't work either. I also tried making my own function to add to the mix and simplify it,</p>
  
  <pre><code>function searchtable(table, element)
    for _, v in pairs(table) do
      if v == element then
        return true
      else
        return false
      end
    end
  end
  
  for _, v in pairs(gmcptable) do
    if searchtable(mytable, v) == true then
      echo("yay")
    else
      echo("no!")
    end
  end
  </code></pre>
  
  <p>that was a bust also... I'm sure I'm just overlooking something or showing what an amateur I am, but I've googled loads and tried everything I can think of, but I'm just self taught and only recently started understanding how tables and for loops even work. Hopefully someone out there can get back to me with something that works soonish!</p>
  
  <p>UPDATE!</p>
  
  <p>@Piglet Okay so, gmcptable was actually me trying to simplify the question for those who could answer the coding question. gmcptable actually is a long list of tables received by my client via the connection from the server the game this is for. so in all actuality, I have 3 tables I'm parsing data from. "gmcp.Char.Items.List.items", "gmcp.Char.Items.Add" and "gmcp.Char.Items.Remove". Now gmcp.Char.Items.List.items is the list of everything in the room I'm in within the game. gmcp.Char.Items.Add is the list of anything that enter the room and is sent each time anything enters the room aside from other players and gmcp.Char.Items.Remove is the same, but for when anything leaves the room. I'm trying to use this information to create a targeting table that will automatically add desired targets to my targeting que and remove them if they are not in the room. the room list (gmcp.Char.Items.List) is updated only when I enter or exit the room and possibly when I look, but for now I'm assuming it doesn't update when I look because that will be a whole other problem to solve later.</p>
  
  <p>I currently have a simple script in what my client ID's as a trigger, this is set to fire once when I log into the game in question and the script define the tables that hold the value's I'm cross referencing the gmcp tables with to figure out if it's information I want added to my target table, this script also defines the target table as empty, which is meant to ensure that for the duration of the session, both tables exist and are defined.</p>
  
  <p>I then added three separate scripts that parse the three gmcp tables and figure out whether that are on my desired targets table and if so adds it or in the of the case of the remove table checks if its currently on the targets table and if so removes it. below I'll show the current scripts I'm using (which have changed several times over since yesterday and might change again before I get a look at any future replies to this. I will also include a what the gmcp tables in question look like and if I'm currently seeing any error or debug details from my client I'll include that as well.</p>
  
  <pre><code>log on trigger
  
  match on &gt; ^Password correct\.     in perl regex
  
  bashtargets = {}
  bashlist = {
  "a baby rat",
  "a young rat",
  "a rat",
  "an old rat",
  "a black rat"
  }
  </code></pre>
  
  <p>(the above trigger appears to be working properly and I can print the tables accurately)</p>
  
  <p>script in the room</p>
  
  <p>event handlers > gmcp.Char.Items.List</p>
  
  <pre><code> for _, v in pairs(gmcp.Char.Items.List.items) do
      bashname = v.name
      bashid = v.id
      for _, v in pairs(bashlist) do
          if v == bashname then
              table.insert(bashtargets, bashid)
          end
      end
  end
  </code></pre>
  
  <p>script addcheck</p>
  
  <p>event handlers  "gmcp.Char.Items.Add"</p>
  
  <pre><code>for _, v in pairs(gmcp.Char.Items.Add) do
      addname = v.name
      addid = v.id
      for _, v in pairs(bashlist) do
          if v == addname then
              table.insert(bashtargets, addid)
          end
      end
  end
  </code></pre>
  
  <p>script removecheck</p>
  
  <p>event handlers "gmcp.Char.Items.Remove"</p>
  
  <pre><code>for _, v in pairs(gmcp.Char.Items.Remove) do
      delid = v.id
      for _, v in pairs(bashtargets) do
          if v == delid then
              table.remove(bashtargets, delid)
          end
      end
  end
  </code></pre>
  
  <p>gmcp table "gmcp.Char.Items"</p>
  
  <pre><code>{
    Remove = {
      location = "room",
      item = {
        id = "150558",
        name = "a filthy gutter mutt",
        attrib = "m"
      }
    },
    Add = {
      location = "room",
      item = {
        id = "150558",
        name = "a filthy gutter mutt",
        attrib = "m"
      }
    },
     List = {
      location = "room",
      items = {
        {
          id = "59689",
          name = "a statue of a decaying gargoyle",
          icon = "profile"
        },
        {
          id = "84988",
           name = "a gas lamp"
         },
         {
          id = "101594",
          attrib = "t",
          name = "a monolith sigil",
          icon = "rune"
        },
        {
          id = "196286",
          name = "a wooden sign"
        },
        {
          id = "166410",
          name = "Lain, the Lamplighter",
          attrib = "m"
        }
      }
    }
  }
  </code></pre>
  
  <p>I have parsed the information successfully several times, so I've got the right tables and syntax and what have you where gmcp is concerned.
  using this I have also managed to get it to half work. currently the set up seems to capture single targets at a time even if there are dozens and add that one, sometimes it oddly enough adds the same target 3 - 5 times for some reason, not sure why, haven't been able to figure it out yet.</p>
  
  <p>these two error messages have been output by my client repeatedly, no idea what to do about them though or how to fix them... "left the room" and "entered the room" are the names currently assigned to the scripts for adding and removing data from the tables in my client.</p>
  
  <pre><code>[ERROR:] object:&lt;event handler function&gt; function:&lt;left the room&gt;
  &lt;Lua error:[string "return left the room"]:1: '&lt;eof&gt;' expected near 'the'&gt;
  [ERROR:] object:&lt;event handler function&gt; function:&lt;entered the room&gt;
  &lt;Lua error:[string "return entered the room"]:1: '&lt;eof&gt;' expected near 'the'&gt;
  </code></pre>
  
  <p>I have no idea what '' means though, or why it's expected near 'the' it's all got my head pounding though...</p>
  
  <p>I can see through the debug feature on my client that all the handlers are being sent by the server so it's not the gmcp...  I'm not actually seeing any bugs on the debug feature (which btw is separate from the error feature that keeps putting out those other two errors I mentions.</p>
  
  <p>anyways that's my update... Hopefully that give some people a better handle on what I'm doing wrong so I can get this figured out and learn something new.  </p>
  
  <p>Thanks again in advance and extra thanks to you @Piglet for you answer I definitely learned something new from it and thought it was very helpful.</p>