Jump to content




Find out the length of longest string in a table


  • You cannot reply to this topic
6 replies to this topic

#1 MatazaNz

  • Members
  • 42 posts
  • LocationNew Zealand

Posted 07 July 2013 - 10:27 PM

I am trying to write a function that prints out a context menu that is sized correctly in height to the amount of options in a set table, and in width to fit the size of the longest string in the same table. Here is an example of the table:

options = {
  "New program"
  "File Manager"
  "Edit program"
}

I have the bulk of the function done, but I don't know how to get my program to work out the length of the longest string in the table. I am doing this so that I can add more options without having to hard-code the menu gui into it. Any help will be appreciated. Thank you

#2 AgentE382

  • Members
  • 119 posts

Posted 07 July 2013 - 10:42 PM

local function longestString(t)
    local longest = 0
    for k, v in pairs(t) do
        local len = #v
        if len > longest then longest = len end
    end
    return longest
end
This assumes, of course, a table containing only strings.

Edited by AgentE382, 07 July 2013 - 11:22 PM.


#3 MatazaNz

  • Members
  • 42 posts
  • LocationNew Zealand

Posted 07 July 2013 - 10:50 PM

View PostAgentE382, on 07 July 2013 - 10:42 PM, said:

function longestString(t)
	local longest = 0
	for k, v in pairs(t) do
		local len = #v
		if len > longest then longest = len
	end
	return longest
end
This assumes, of course, a table containing only strings.

Cool, thank you. How would I call this in my code? I have something like this,
for i = 1, #<longest string here> do
  <code>
end

Would I just call "longest", or would I call the function longestString?

#4 AgentE382

  • Members
  • 119 posts

Posted 07 July 2013 - 11:02 PM

View PostMatazaNz, on 07 July 2013 - 10:50 PM, said:

View PostAgentE382, on 07 July 2013 - 10:42 PM, said:

function longestString(t)
	local longest = 0
	for k, v in pairs(t) do
		local len = #v
		if len > longest then longest = len
	end
	return longest
end
This assumes, of course, a table containing only strings.

Cool, thank you. How would I call this in my code? I have something like this,
for i = 1, #<longest string here> do
  <code>
end

Would I just call "longest", or would I call the function longestString?

You would call it like this:
for i = 1, longestString(tablename) do
  <code>
end
Oh, yeah. I edited my previous post. Use that version instead. It will be marginally faster in most cases.

#5 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 07 July 2013 - 11:04 PM

For the record, this needs commas (or semicolons):
options = {
  "New program",
  "File Manager",
  "Edit program"
}

And this needs an end to the if:
        if #v > longest then longest = #v end

Also, AgentE's function only finds the longest string length, and not the longest string. If you want the longest string:
function longestString(tab)
  local longest
  for i=1, #tab do
    local str = tab[i]
    if not longest or #str > #longest then
      longest = str
    end
  end
  return longest
end
His solution would still solve your problem, though. Just wanted to throw this out there in case you actually need the longest string. A better name for his function would be "longestLength".

As a side note, I use a numeric loop instead of pairs in this code, simply because it can be a little faster.

#6 MatazaNz

  • Members
  • 42 posts
  • LocationNew Zealand

Posted 07 July 2013 - 11:18 PM

View PostKingdaro, on 07 July 2013 - 11:04 PM, said:

For the record, this needs commas (or semicolons):
options = {
  "New program",
  "File Manager",
  "Edit program"
}

And this needs an end to the if:
		if #v > longest then longest = #v end

Also, AgentE's function only finds the longest string length, and not the longest string. If you want the longest string:
function longestString(tab)
  local longest
  for i=1, #tab do
	local str = tab[i]
	if not longest or #str > #longest then
	  longest = str
	end
  end
  return longest
end
His solution would still solve your problem, though. Just wanted to throw this out there in case you actually need the longest string. A better name for his function would be "longestLength".

As a side note, I use a numeric loop instead of pairs in this code, simply because it can be a little faster.
I was only looking for the length of the longest string, simply to determine the size width-wise of the box. And the table I wrote was wrong, I know, I just wrote it out quickly for the purpose of this post.

#7 AgentE382

  • Members
  • 119 posts

Posted 07 July 2013 - 11:19 PM

Thanks for spotting that typo. Editing my post to reflect that.

A rename would probably make things a bit clearer.

I used a pairs for loop because I know nothing about the rest of his API. However, from his example, I should have made that assumption.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users