Sobsno there is such a gruel, as will be presented below. Improvement notes and constructive criticism are always held in high esteem. The question is, is there a possibility to determine the position of an element in an array, somehow without shamanism, and now for this I use something like:

function minArr(arr,value) -- сравнСниС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ с Ρ‚Π°Π±Π»ΠΈΡ†Π΅ΠΉ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ для полуСния Π΅Π³ΠΎ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ Π² Ρ‚Π°Π±Π»ΠΈΡ†Π΅ for i = 1, #arr do if(arr[i] == value) then return i end end end 

and the whole code:

  points = {} -- собсна Ρ‚ΠΎΡ‡ΠΊΠΈ (значСния ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½ΠΎ ΠΈΠ· Π΄Ρ€ΡƒΠ³ΠΎΠ³ΠΎ источника) --[[ Ρ‚Π°Π±Π»ΠΈΡ†Π° Ρ‚ΠΎΡ‡ΠΊΠΈ являСт собой Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ Ρ‚Π°Π±Π»ΠΈΡ† с ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ΠΌ Π΅Π΄ΠΈΠ½ΠΈΡ‡Π½Ρ‹Ρ… Ρ‚ΠΎΡ‡Π΅ΠΊ points[1] = point1 --> point1 = {}; point1.x = value point1.y = value points[2] = point2 --> point2 = {}; point2.x = value point2.y = value points[3] = point3 --> point3 = {}; point3.x = value point3.y = value ΠΈ Ρ‚Π΄ ]] local marks = {} -- ΠΌΠ΅Ρ‚ΠΊΠΈ Ρ‚ΠΎΡ‡Π΅ΠΊ занята/свободна for i = 1, #points do -- ΠΈΠ·Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ всС Ρ‚ΠΎΡ‡ΠΊΠΈ свободны marks[i] = false end function path(arr) -- собсна ΡˆΡƒΠΊΠ°Π΅ΠΌ ΠΏΡƒΡ‚ΡŒ local p_arr = {} p_arr = arr local counter = 1 local minim = 0 local position = 0 local length_arr ={} -- Ρ‚Π°Π±Π»ΠΈΡ†Π° Π΄Π»ΠΈΠ½ для Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ while(counter ~= #p_arr ) do marks[counter] = true -- сразу ΠΌΠ°Ρ€ΠΊΠΈΡ€ΡƒΠ΅ΠΌ Ρ‚Π΅ΠΊΡƒΡ‰ΡƒΡŽ Ρ‚ΠΎΡ‡ΠΊΡƒ ΠΊΠ°ΠΊ Π·Π°Π½ΡΡ‚ΡƒΡŽ for i = 1, #p_arr do -- посчитали, записали Π΄Π»ΠΈΠ½Ρ‹ ΠΈΠ· Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΊ ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹ΠΌ length_arr[i] = leng(p_arr[counter], p_arr[i]) end minim = compare(length_arr) -- нашли ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌ ΠΈΠ· посчитаных Π΄Π»ΠΈΠ½ position = minArr(length_arr,minim) -- нашли ΠΏΠΎΠ·ΠΈΡ†ΠΈΡŽ ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌΠ° Π² Ρ‚Π°Π±Π»ΠΈΡ†Π΅ Π΄Π»ΠΈΠ½ if(marks[position] == true) then -- Ссли Π²Π΅Ρ€ΡˆΠΈΠ½Π° занята Ρ‚ΠΎ ΠΏΡ€ΠΎΠ²ΠΎΠ΄ΠΈΠΌ поиск ΠΌΠΈΠ½ΠΈΠΌΡƒΠΌΠ° ΠΏΠΎ Π½ΠΎΠ²ΠΎΠΉ while(marks[position] == true) do length_arr[position] = math.huge minim = compare(length_arr) position = minArr(length_arr,minim) end print("nearest to point " .. counter .. " is " .. position) else -- Ссли Π²Π΅Ρ€ΡˆΠΈΠ½Π° свободна Ρ‚ΠΎ Π΅Π΄Π΅ΠΌ дальшС print("nearest to point " .. counter .. " is " .. position) end counter = counter + 1 end end function minArr(arr,value) -- сравнСниС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ с Ρ‚Π°Π±Π»ΠΈΡ†Π΅ΠΉ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ для полуСния Π΅Π³ΠΎ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ Π² Ρ‚Π°Π±Π»ΠΈΡ†Π΅ for i = 1, #arr do if(arr[i] == value) then return i end end end function compare(arr) -- сравнСниС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ для получСния наимСньшСго local minim = math.huge for i = 1, #arr do if minim > arr[i] then minim = arr[i] end end return minim end function leng(point1, point2) -- расчСт Π΄Π»ΠΈΠ½Ρ‹ ΠΌΠ΅ΠΆΠ΄Ρƒ Ρ‚ΠΎΡ‡ΠΊΠ°ΠΌΠΈ local n = 0 n = math.sqrt(math.pow((point2.x - point1.x),2) + math.pow((point2.y - point1.y),2)) return n end 

    1 answer 1

    In lua arrays are tables and there is no concept like "index", there are concepts of key and value . Accordingly, to get the key by value, there is nothing better than iterating:

     function get_key_for_value(t, value) for k,v in pairs(t) do if v==value then return k end end return nil end 

    If you have a fixed table and you need to frequently look for keys, then you can make an inverted table and look for it:

     function table_invert(t) local s={} for k,v in pairs(t) do s[v]=k end return s end 

    But you are in for a surprise if your table has several identical values ​​for different keys.

    • Well, it's a pity, but thanks to that too - DuckImMud