Domanda

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?

È stato utile?

Soluzione 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) )

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top