How to get the bigger string of a table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Cauan
Prole
Posts: 22
Joined: Thu Mar 09, 2023 5:58 pm

How to get the bigger string of a table

Post by Cauan »

Hello!

How to get the bigger string of a table

For example:

Code: Select all

values = {'FirstString', 'SecondString', 'Third String', 'FourString', 'the bigger string of this table'}
How to me get the most bigger string of that table with code?
I'm me, and you are you
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to get the bigger string of a table

Post by darkfrei »

Cauan wrote: Tue Sep 05, 2023 5:50 pm Hello!

How to get the bigger string of a table

For example:

Code: Select all

values = {'FirstString', 'SecondString', 'Third String', 'FourString', 'the bigger string of this table'}
How to me get the most bigger string of that table with code?

Code: Select all

local function getLongestString (list)
  local length, longestStr = 0
  for i, str in ipairs (list) do
    local len = str:length ()
    if len > length then
      longestStr = str
    end
  end
  return longestStr
end
    
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Cauan
Prole
Posts: 22
Joined: Thu Mar 09, 2023 5:58 pm

Re: How to get the bigger string of a table

Post by Cauan »

So we will get this code:

Code: Select all

--just one modification
function add.getLongestString(list)
  local length, longestStr = 0
  for i, str in ipairs (list) do
    local len = #str
    if len > length then
      length = len
      longestStr = str
    end
  end
  return longestStr
end
That worked to me! Thank you :awesome: :ultrahappy:
I'm me, and you are you
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests