Jump to content
The simFlight Network Forums

Recommended Posts

Posted

Hello together, ist's me again. :-)

With LUA we are able to write Data into a TextFile. But, is it also possible to read Data from a TextFile and assign the getted Value to a Variable? If it is possible, is there an Example, how to code it in LUA? Thanks in Avance again for your Answers.

Joachim

Posted

Thanks for your Reply.

The Value comes from an external Program (MS Access) and will be saved in B777FuelQty.txt. It is the only Entry in this File.

Now i will read this with

file = io.open("B777FuelQty.txt","r")

InitialFuelQty = file:read()

file:close()

Let's say, the FileEntry is 15000. Is the above Code enough to assign 15000 in the File to the Variable InitialFuelQty in LUA?

If so, how do i read more Entrys in a File, i.E. Line by Line or comma separated?

 

 

 

Posted
2 hours ago, Airbuspilot said:

file = io.open("B777FuelQty.txt","r")

InitialFuelQty = file:read()

file:close()

Let's say, the FileEntry is 15000. Is the above Code enough to assign 15000 in the File to the Variable InitialFuelQty in LUA?

Yes, that should work, just as my script works.........

 

2 hours ago, Airbuspilot said:

If so, how do i read more Entrys in a File, i.E. Line by Line or comma separated?

In a subsequent post in the same thread:

http://forum.simflight.com/topic/77525-updating-radio-frequencies-for-wideview-network/?do=findComment&comment=470781

I have shown how several values can be written as a code string, just numbers with non-number separators......I used pure text separators like "COMone=". I have also shown how that string can be decoded.

Posted

i saw that Post, but did not understand the Lua-Code for decoding the String.

Are Frequencies not all in the same Length? My FuelFields could vary in their Length. Could this be handled with your Code?

Posted

The length does not matter, but the data must be purely numerical, so decimal dots or commas would not be acceptable. In my code you can see that 'battery' has a different length.

Posted

I am back again with a Question.

Read and Assign of the Data from the File will work fine.

But, these are FuelData and have to be numerics, because i have to do further Calculations with them.

When i run the Lua, this Error occurs:112422 *** LUA Error: D:\Simulatoren\Fliegen\P3Dv32\Modules\FlightLog.lua:163: attempt to compare number with string

Is it possible, to read out the Data from the File as Numerics? And if so, how have i to do it.? Thanks in Advance for your Help.

Posted

Lua 5.1 has a tonumber() function which might do what you are seeking. Please see:

http://www.lua.org/manual/5.1/manual.html#pdf-tonumber

Quote

tonumber (e [, base])

Tries to convert its argument to a number. If the argument is already a number or a string convertible to a number, then tonumber returns this number; otherwise, it returns nil.

An optional argument specifies the base to interpret the numeral. The base may be any integer between 2 and 36, inclusive. In bases above 10, the letter 'A' (in either upper or lower case) represents 10, 'B' represents 11, and so forth, with 'Z' representing 35. In base 10 (the default), the number can have a decimal part, as well as an optional exponent part (see §2.1). In other bases, only unsigned integers are accepted.

tostring (e)

Receives an argument of any type and converts it to a string in a reasonable format. For complete control of how numbers are converted, use string.format.

If the metatable of e has a "__tostring" field, then tostring calls the corresponding value with e as argument, and uses the result of the call as its result.

for example:

> = tonumber("123") + 25
148
> x = tonumber("123.456e5")
> print(x)
12345600
> y = "234"
> z = tonumber(y)
> print(z+10)
244

 

Posted

Lua also has a feature called Coercion, but that does not work with comparison operators, and that is probably why you saw the error you reported. From http://lua-users.org/wiki/NumbersTutorial:

Quote

Coercion

Lua will automatically convert string and number types to the correct format to perform calculations. For example: if you try to apply an arithmetic operation to a string Lua will try to convert that string to a number first. Otherwise the operation will not work. If the string cannot be converted to a number an error is raised. This automatic conversion of types is called coercion.

 


> = 100 + "7"
107
> = "1000" + 234
1234
> = "hello" + 234
stdin:1: attempt to perform arithmetic on a string value
stack traceback:
        stdin:1: in main chunk
        [C]: ?
> = 234 + "1000"
1234

You can see the calculation succeeds where a string was converted to number. The string "hello" cannot be converted to a number and so an error occurs. In statically typed languages (e.g. C) this would cause an error because you cannot assign a value to a variable of an incompatible type without a cast. This works in Lua because it is dynamically typed.

A notable exception: comparison operators (== ~= < > <= >=) do not coerce their arguments. The (in)equality operators consider a number to be not equal to its string representation (or any non-number type in fact). Ordered comparison operators throw an error when you feed them different types.

 

> = 100 == "100"
false
> = 100 ~= "hello"
true
> = 100 ~= {}
true
> = 100 == tonumber("100")
true
> = 100 <= "100"
stdin:1: attempt to compare number with string
stack traceback:
        stdin:1: in main chunk
        [C]: ?

For performance reasons you should avoid relying on automatic coercion too much. Make sure that all numbers in performance sensitive computations (especially in inner loops) are of the proper type.

 

Posted

That is the Problem. I have to do Compare OPs, to avoid LUA Errors on arithmetic OPs or dividing by Zero Errors.

So, when reading the Initial Fuel Data from the File, i got the calculated Fuel for the Flight and can do all further Calculations with this Value.

This Problem occurs only with the PMDG777. So i think, i had find a Solution, that i will try this Morning. Hope, it works.

Have a nice Sunday and thanks for your Answer.

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
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.