This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MatazaNz's profile picture

Find out the length of longest string in a table

Started by MatazaNz, 07 July 2013 - 09:27 PM
MatazaNz #1
Posted 07 July 2013 - 11: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
AgentE382 #2
Posted 07 July 2013 - 11: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 on 07 July 2013 - 10:22 PM
MatazaNz #3
Posted 07 July 2013 - 11:50 PM

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?
AgentE382 #4
Posted 08 July 2013 - 12:02 AM

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.
Kingdaro #5
Posted 08 July 2013 - 12:04 AM
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.
MatazaNz #6
Posted 08 July 2013 - 12:18 AM
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.
AgentE382 #7
Posted 08 July 2013 - 12:19 AM
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.