Question

Situation :

I'm coding about saving the data which is values of the sensors.

How can I save two values in one sentence, the line below makes error

filehandle:write(" -The Value of the Sensors : "..tostring(valueOfSensor_1)..tostring(valueOfSensor_2).."\n")

I want to save the record like

-The value of the Sensors : 13 and 20

-The value of the Sensors : 14 and 24

-The value of the Sensors : 16 and 22

-The value of the Sensors : 42 and 12

like this.. Any suggestions please?

Était-ce utile?

La solution 2

The best method(imho) is to use string.format

local MyString = "-The value of the Sensors : %d and %d\n"
filehandle:write( MyString:format(valueOfSensor_1, valueOfSensor_2) )

Autres conseils

I'm not sure what you mean by "makes error", but I think what you want is:

filehandle:write(" -The Value of the Sensors : "..tostring(valueOfSensor_1).." and "..tostring(valueOfSensor_2).."\n")

ie what you were doing was just putting the two numbers as strings together, what you want is to insert the string " and " between them.

You could also do

filehandle:write(" -The Value of the Sensors : ", valueOfSensor_1, " and ", valueOfSensor_2, "\n" )

as write will automatically convert to string all arguments and concaternate them much like you were with the .. operation.

Only other thing to check is that you're opening the file with "w" (write) as the mode and not "r" (read) and also that the open is working (that the file handle isn't nil).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top