lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2017-05-15 10:10 GMT+02:00 Jorge Eduardo de Araújo Oliveira
<jorgeeduardoaoliveira@gmail.com>:
> Wow!
> I'm sorry...
> The right code is it:
>
> function linearSearch(table,element)
> local position=0
> while position<#table do
> if table[position]==element then
> return position
> else
> position=position+1
> end
> end
> end

It will interrogate position[0] to position[#table-1].

But why do it this way? Idiomatic Lua is:

function linearSearch(table,element)
  for k,v in pairs(table) do
    if v==element then return k
  end
end