Jump to content

Tables - two questions.


ds1-e

Recommended Posts

  • Scripting Moderators

Hey, today a problem appeared, i need to do something but i don't know how i can solve it.

1. How can i get text length of everything what's in the table.

2. If length atleast of one value from table will be >= 25 then do something for everything.

Code:
 

local textTable = 
{
	"abcdefghijklmnopqrstu", -- 21 
	"abcdefghijklmnopqrstu123", -- 24 
	"abcdefghijklmnopqrstu124", -- 25
}

addEventHandler("onClientRender", getRootElement(), function()
	if getPedOccupiedVehicle(getLocalPlayer()) then return end
	for k, v in pairs(textTable) do
		dxDrawingColorTextMenuScroll(v, 6, 350 + k * boxSpace, 6, 350 + (k + 1) * boxSpace, tocolor(255, 255, 255, 255), 170, 1, "sans", "left", "center")
		if string.len(v) >= 25 then -- problem is here, this works only for the value which have text >= 25
		-- do something for everything in table
	else
		-- otherwise
	end
end
end)

 

Edited by majqq
Link to comment
  • Scripting Moderators
Just now, nurfaizfy19 said:

I think your code is correct, but you missed one string 

What? Code is correct, but i need to make it on other way, this is no answer for my questions.

Link to comment
  • Scripting Moderators
1 minute ago, DNL291 said:

I really didn't understand what your issue is. What exactly do you want to do in the scope at line 12?

 

I need to get length of all values from table. If atleast one value text length it's >= 25 then i need to do something. Sorry i can't explain it better for now.

Link to comment

I think I understood it now.

  • Getting the length of all values from table:
local totalLength = 0
for _,s in ipairs(textTable) do
	totalLength = totalLength + s:len()
end
print( totalLength )
  • Checking if at least one value is >= than 25:
local textTable = { "abcdefghijklmnopqrstu", "abcdefghijklmnopqrstu123", "abcdefghijklmnopqrstu124", }

local lengthsGreaterThan25 = 0
for _,s in ipairs(textTable) do
	if s:len() >= 25 then
		lengthsGreaterThan25 = lengthsGreaterThan25 + 1
	end
end

print( lengthsGreaterThan25 )

 

 

  • Thanks 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...