This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
westloar's profile picture

Searching through tables

Started by westloar, 30 July 2015 - 05:48 PM
westloar #1
Posted 30 July 2015 - 07:48 PM
I am looking to write a function to search through a pre-made table and pull out the related data and was thinking of using a while loop to search through each entry of the table:



local function searchTable(searchTerm)

i = 0
local finished = false

  while finished ~= true do
  i = i +1
  if table[i][3] == "searchTerm" then

  item1 = table[i][2]
  item2 = table[i][4]

  return item1, item 2
  finished = true
  end

end


First of all, I am new to Lua and pretty new to programming in general, would this code work?

Second, is there a hard-coded way/api/method to search through arrays/tables?

Thanks in advance!
Edited on 30 July 2015 - 05:50 PM
KingofGamesYami #2
Posted 30 July 2015 - 11:36 PM
For loops are useful here as is the pairs function, it shortens your code considerably:


local function searchTable(searchTerm)
  for k, v in pairs( tbl ) do --#NEVER call your tables 'table'!  'table' is an API.
    if v[3] == searchTerm then
       return v[2], v[4]
    end
  end
end

As to the validity of your code, it will error on line 14 or 15, because you cannot do things in a function after you return. In addition, setting a variable isn't necessary, because return ends the loop (and the function).
Bomb Bloke #3
Posted 31 July 2015 - 02:52 AM
Alternatively, instead of numerically indexing your values:

local tbl = {{1,2,"name",4}, {1,2,"someothername",4}, {1,2,"yetanothername",4}}

… you can use those "name" strings as keys to index them:

local tbl = {["name"] = {1,2,3}, ["someothername"] = {1,2,3}, ["yetanothername"] = {1,2,3}}

This allows you to get the values you want without looping at all; tbl[searchTerm][2] & tbl[searchTerm][3] lead you directly to them.
westloar #4
Posted 31 July 2015 - 06:53 AM
For loops are useful here as is the pairs function, it shortens your code considerably:


local function searchTable(searchTerm)
  for k, v in pairs( tbl ) do --#NEVER call your tables 'table'!  'table' is an API.
	if v[3] == searchTerm then
	   return v[2], v[4]
	end
  end
end

As to the validity of your code, it will error on line 14 or 15, because you cannot do things in a function after you return. In addition, setting a variable isn't necessary, because return ends the loop (and the function).

The first code I wrote for this before asking was a for loop, but I wasn't sure that return would end the function (as I said, noob :P/>). The names are all placeholders, don't worry I learnt my lesson about variable nomenclature when I accidentally overwrote the whole colors API with a table!

As for the code you supplied that's very helpful and definitely shorter, thank you. Just for my own peace of mind would my code of worked aside from the error after return?

Alternatively, instead of numerically indexing your values:

local tbl = {{1,2,"name",4}, {1,2,"someothername",4}, {1,2,"yetanothername",4}}

… you can use those "name" strings as keys to index them:

local tbl = {["name"] = {1,2,3}, ["someothername"] = {1,2,3}, ["yetanothername"] = {1,2,3}}

This allows you to get the values you want without looping at all; tbl[searchTerm][2] & tbl[searchTerm][3] lead you directly to them.

This is definitely knowledge to add to my repertoire, although with this table there will be multiple types of data I would like to search by, so this method may not be appropriate.

Regardless, thank you, this forum is probably one of the friendliest and most knowledgeable I have joined in a long time, I am learning alot :)/>
Bomb Bloke #5
Posted 31 July 2015 - 08:08 AM
Just for my own peace of mind would my code of worked aside from the error after return?

Other than that, and the bit where you typed "item 2" instead of "item2", it looks functional to me.