This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
digitalEntity's profile picture

file read used as a function

Started by digitalEntity, 05 April 2014 - 09:55 PM
digitalEntity #1
Posted 05 April 2014 - 11:55 PM
So I've recently been thinking about a way to make a more modular and customizable form of a security system I'm working on (previously mentioned here) in which a user inputs a password, which then gets converted to a number, based on the numerical value attached (by the system) to each key that is pressed. Then, said number is processed through a somewhat complex algebraic equation in order to change it to something that can't simply be pulled up next to a keycode chart and translated directly into a password by an unwanted third party, who "happens to have received this transmission." *angrily grumbles something about dirty hackers*

Anywho, I was wondering how to make the system more, well, modular, meaning that there would be two files: one containing the actual meat and muscle of the program, which acts on user input etc, etc, but is never changed, and the other file contains lines that are to be applied to variables in the program. But, one of these lines that I need to apply to a variable is the algebraic equation, which, according to the Wiki gets read as a string. Well, "result = '((-4)(x)((3+2(x))^2)'" isn't going to work, because then the result is just gonna be a printed string, instead of a processed number.

Is there a way to convert this to… a less mathematically challenged form of input lol?

a sample of my current code is something like this:


var = 45
x = var
filehandle = fs.open("beta", "r")
equation = filehandle.readLine()
n = tonumber(equation)

context: the program itself is called alpha, and the file with the list of variables is called beta. The equation is the first line of beta.

I used the (current) real equation above: ((-4)(x)((3+2(x))^2)
CometWolf #2
Posted 06 April 2014 - 12:25 AM
Storing the equation along with the encoded numbers defeats the whole purpose of encoding them, no? Why not just keep it in the actual program? Anyways, if i recall correctly tonumber() can be used to convert string equations to the resulting numbers.
Link149 #3
Posted 06 April 2014 - 07:56 PM
If you intend to store information in another file in
valid Lua syntax then would this:


dofile("filename")

or maybe this:


pcall(function() loadfile("filename")() end)

be of any help to you ? I think you could also use os.loadAPI("filename")
but I think the major difference is that you wouldn't be able to access to local fields. Please correct me if I'm wrong.

Also, tonumber() wouldn't work as this equation you provided is not syntaxically correct Lua.
"result = (-4 * x * math.pow(3+2 * x, 2)" would probably do, though.
Edited on 06 April 2014 - 05:57 PM
CometWolf #4
Posted 06 April 2014 - 08:16 PM
"^" Is a valid arithmetic Lua operator, idk why math.pow is even a function to be honest. You're probably right about the multiplication signs though. dofile/loadfile would work, provided he adds a return statement to the end of the file returning the result of the equation.
RoD #5
Posted 06 April 2014 - 09:54 PM
"^" Is a valid arithmetic Lua operator, idk why math.pow is even a function to be honest. You're probably right about the multiplication signs though. dofile/loadfile would work, provided he adds a return statement to the end of the file returning the result of the equation.
Actually you can use it, try load lua interactive prompt and type:
2^5
It will give 32, wich is:
2*2*2*2*2
So it is a valid operator. :P/>

Edit: My bad instead of "Valid" i read "invalid" sorry
Edited on 06 April 2014 - 07:55 PM
Link149 #6
Posted 19 April 2014 - 03:34 AM
"^" Is a valid arithmetic Lua operator, idk why math.pow is even a function to be honest.

Oops, my bad. I'm learning a lot of programming and scripting langages at once so I sometimes get confused about the meaning of the operators. I thought it was a bitwise operator or something, just like C/C++ or Java. Well, in that case, "^" is probably just short-hand for "math.pow(x, y)", much like the length operator ("#") and the "table.getn()" function are. That would explain why "math.pow" exists.


EDIT:

Found on the Roblox wiki:

math.sqrt (x)

Returns the square root of x. You can also use the expression x^0.5 to compute this value, but it is less efficient.
Edited on 21 April 2014 - 06:20 PM