Lua: Is there a way to combine null values?

I have the following function in Lua:

function iffunc(k,str,str1) if k ~= 0 then return str .. k .. (str1 or "") end end 

This function allows me to check if the value of k is full or not. I actually use it to determine if I want to display something that has a null value. My problem is this: I am trying to concatenate the iffunc () string, but since some of the values ​​are 0, it returns an error when trying to match the nil value. For instance:

 levellbon = iffunc(levellrep["BonusStr"],"@ wStr@r {@x111","@r}") .. iffunc(levellrep["BonusInt"],"@ wInt@r {@x111","@r}") .. iffunc(levellrep["BonusWis"],"@ wWis@r {@x111","@r}") 

If any of the table values ​​is 0, it will return an error. I could easily put "return 0" in iffunc itself; however, I also do not want a string of thousands. So, how can I work, where no matter what values ​​are zero, I will not get this error? Ultimately, I'm going to make an iffunc in the levellbon variable to see if it is full or not, but I have this part. I just need to overcome this little obstacle right now. Thanks!

+4
source share
2 answers

I would do this:

 function iffunc(k,str,str1) if k == 0 then return "" end return str .. k .. (str1 or "") end 
+18
source

You must add an else to the function where you return an empty string ( "" ).

+1
source

Source: https://habr.com/ru/post/1380160/


All Articles