Jump to content

Tables - two questions.


sacr1ficez

Recommended Posts

Posted (edited)

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
Posted
22 minutes ago, majqq said:
  • local textTable =
  • {
  • "abcdefghijklmnopqrstu", -- 21
  • "abcdefghijklmnopqrstu123", -- 24
  • "abcdefghijklmnopqrstu124", -- 25 -- this string length is the same as above
  • }

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

Posted
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.

Posted

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

Please do not PM me with scripting related question nor support, use the forums instead.

Posted
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.

Posted

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

Please do not PM me with scripting related question nor support, use the forums instead.

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...