Jump to content
  • 0

Search within array value and location


Rdosk

Question

Hi, sorry for my English. I need help. I need to make a list with a filter for a script. As you can see in image 1, there is an "array" that I need to search for by name (I put those positions (1000,1001...) because they are identifiers of its name), depending on what is placed in a gg . request and print the position and value (name).

image 1:

imagen.thumb.png.06f29df916e5a28a38b58bf4dd4c308c.png

 

image 2:

imagen.thumb.png.f6b506e3e13d52a7fa32979bdcba8e7f.png

 

They are examples of what I tried but I can't get it to show me regardless of the position in the array.
ahem: if I look in the gg.prompt nature, it does find it, but if I look for fire to simplify the "game bird" thing in the array, I can't find it

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

you can use this helper

-- Search for a value in an array and return the index and value if found
-- @param arr The array to search index
-- @param callback The callback function to use for searching
-- @return The index and value if found, nil otherwise
local function array_find(arr, callback)
  for i, v in pairs(arr) do
    if callback(v, i) then
      return i, v
    end
  end
end

-- Iterate over an array and return a new array with the results of the callback
-- @param arr The array to iterate over indexes
-- @param callback The callback function to use for mapping
-- @return The new array with the results of the callback
local function array_map(arr, callback)
  local new_arr = {}
  for i, v in pairs(arr) do
    new_arr[i] = callback(v, i)
  end

  return new_arr
end

-- Iterate over an array and return true if the value passes the callback test and false otherwise
-- @param arr The array to iterate over indexes
-- @param callback The callback function to use for testing
-- return true if the value passes the callback test and false otherwise
local function array_every(array, callback)
  for i, v in pairs(array) do
    if not callback(v, i) then return false end
  end
  return true
end

-- Iterate over an array and return a new array with the values that pass the callback test
-- @param array The array to iterate over indexes
-- @param callback The callback function to use for testing
-- return A new array with the values that pass the callback test
local function array_filter(array, callback)
  local new_arr = {}
  for i, v in pairs(array) do
    if callback(v, i) then new_arr[i] = v end
  end
  return new_arr
end

They are implementation of native function that exist in JavaScript they are very useful

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Link to comment
Share on other sites

In your case you should use the array_find function like this, this will return the first occurrence in the table that have the value of natura

local id, name = array_find(arrayName, function(v, i)
  return v == "natura"
end)

print(id, name)

 

Link to comment
Share on other sites

Thank you very much, and in the event that I need to look for a fire bird (array[1001]="pajaro de fuego"), but I want it to be found just by typing fire ("fuego"), is that possible?

image.png.1fb877906d7d194be069b5b3019c3431.png

 

On 1/17/2023 at 1:01 PM, MAARS said:

In your case you should use the array_find function like this, this will return the first occurrence in the table that have the value of natura

local id, name = array_find(arrayName, function(v, i)
  return v == "natura"
end)

print(id, name)

 

Thank you very much, and in the event that I need to look for a fire bird (array[1001]="pajaro de fuego"), but I want it to be found just by typing fire ("fuego"), is that possible?image.png.73d317cffa284b2bdd9632e0983a54d3.png

Link to comment
Share on other sites

Hi, since your code is really based on table i will suggest you using this helper to make your life easy, you can split it into module to make your code more readable
 

---@class tablelib
local table = table -- Make a local copy of the table library to make it faster to access.

---Create a new table with the table metatable.
---@return tablelib @The new table.
table.new = function()
  return setmetatable({}, { __index = table })
end

---Return the index of the first value in the table that matches the given value.
---@generic T : number | string
---@param self  table<T, any>
---@param value any
---@return any
---@return T
table.find = function(self, value)
  for i, v in pairs(self) do
    if v == value then
      return v, i
    end
  end
  return nil, nil
end


---Return the index of the first value in the table that matches the given pattern.
---@generic T : number | string
---@param self  table<T, any>
---@param value any
---@return any
---@return T
table.match = function(self, value)
  for i, v in pairs(self) do
    if v:match(value) then
      return v, i
    end
  end
  return nil, nil
end

---Insert mutiple key/value pairs to the table at once.
---@generic T : number | string
---@param self table<T, any>
---@vararg T
table.pushs = function(self, ...)
  local args = { ... }
  for i = 1, #args, 2 do
    self[args[i]] = args[i + 1]
  end
end

---Insert a value to the table at the given index.
---@generic T : number | string
---@param self  table<T, any>
---@param index T
---@param value any
---@overload fun(self, value: any)
table.push = function(self, index, value)

  if not value then
    value = index
    table.insert(self, value)
    return
  end
  self[index] = value
end

---Create your table (table.new instead of using = {})
local t = table.new()

-- Insert key/value pairs to the tables one by one
-- t:push(1000, "natura")
-- t:push(1001, "pajaro de fuego")
-- t:push(1002, "mercurio")
-- t:push(1003, "chicle")
-- t:push(1004, "pez linterna")
-- t:push(1005, "tropical")
-- t:push(1006, "zombi")

--OR

-- Insert mutiple key/value pairs to the table at once
t:pushs(
  1000, "natura",
  1001, "pajaro de fuego",
  1002, "mercurio",
  1003, "chicle",
  1004, "pez linterna",
  1005, "tropical",
  1006, "zombi"
)

--Find the first value that matches the given value
local name, id = t:find("mercurio")
print(name, id)

--Find the first value that matches the given pattern
local name, id = t:match("fuego")
print(name, id)

 

Link to comment
Share on other sites

7 hours ago, MAARS said:

Hi, since your code is really based on table i will suggest you using this helper to make your life easy, you can split it into module to make your code more readable
 

---@class tablelib
local table = table -- Make a local copy of the table library to make it faster to access.

---Create a new table with the table metatable.
---@return tablelib @The new table.
table.new = function()
  return setmetatable({}, { __index = table })
end

---Return the index of the first value in the table that matches the given value.
---@generic T : number | string
---@param self  table<T, any>
---@param value any
---@return any
---@return T
table.find = function(self, value)
  for i, v in pairs(self) do
    if v == value then
      return v, i
    end
  end
  return nil, nil
end


---Return the index of the first value in the table that matches the given pattern.
---@generic T : number | string
---@param self  table<T, any>
---@param value any
---@return any
---@return T
table.match = function(self, value)
  for i, v in pairs(self) do
    if v:match(value) then
      return v, i
    end
  end
  return nil, nil
end

---Insert mutiple key/value pairs to the table at once.
---@generic T : number | string
---@param self table<T, any>
---@vararg T
table.pushs = function(self, ...)
  local args = { ... }
  for i = 1, #args, 2 do
    self[args[i]] = args[i + 1]
  end
end

---Insert a value to the table at the given index.
---@generic T : number | string
---@param self  table<T, any>
---@param index T
---@param value any
---@overload fun(self, value: any)
table.push = function(self, index, value)

  if not value then
    value = index
    table.insert(self, value)
    return
  end
  self[index] = value
end

---Create your table (table.new instead of using = {})
local t = table.new()

-- Insert key/value pairs to the tables one by one
-- t:push(1000, "natura")
-- t:push(1001, "pajaro de fuego")
-- t:push(1002, "mercurio")
-- t:push(1003, "chicle")
-- t:push(1004, "pez linterna")
-- t:push(1005, "tropical")
-- t:push(1006, "zombi")

--OR

-- Insert mutiple key/value pairs to the table at once
t:pushs(
  1000, "natura",
  1001, "pajaro de fuego",
  1002, "mercurio",
  1003, "chicle",
  1004, "pez linterna",
  1005, "tropical",
  1006, "zombi"
)

--Find the first value that matches the given value
local name, id = t:find("mercurio")
print(name, id)

--Find the first value that matches the given pattern
local name, id = t:match("fuego")
print(name, id)

 

Thank you very much, you helped me a lot.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.