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

[lua] variable question

Started by Engineer, 31 January 2013 - 08:44 AM
Engineer #1
Posted 31 January 2013 - 09:44 AM
Hi,

I got a little (noobish) question about variables.

var1 = true -- or false
var2 = 1     -- any number
var3 = "string" -- any possible string
var4 = left   -- I dont think this is even possible, but my question is: Can I set 'made-up' values? (Logical I dont think so, but want to be sure)

Thanks in advance once again :)/>
Okyloky9 #2
Posted 31 January 2013 - 10:01 AM
Are you are using var4 as a side? If so, I would recommend a table (array) with all the sides you would be using:

var4 = {"left","right","up","down","front","back"}

If you aren't using it like this, then, explain what you mean more by "made-up" variables.

–Btw, completely off topic, but I find myself proud of it, 50 posts.
Engineer #3
Posted 31 January 2013 - 10:08 AM
Are you are using var4 as a side? If so, I would recommend a table (array) with all the sides you would be using:

var4 = {"left","right","up","down","front","back"}

If you aren't using it like this, then, explain what you mean more by "made-up" variables.

–Btw, completely off topic, but I find myself proud of it, 50 posts.

Thank you for the response.
I am not using this for sides, but for the MiscPeripheral interactive sorter. East, South, North and west all have different outputs

I now made this:


local directions = {
	-- test = {right, left, forward, back}
	"east" = {3, 2, 5, 4},
	"west" = {2, 3, 4, 5},
	"north" = {5, 4, 2,3},
	"south" = {4, 5, 2,3}
}

If this works with:

directions["north"]

But if not, please leave suggestions how I should do this else.

Thanks.

Off topic: nice Okyloky9 :)/>

EDIT: Dont know where my head was, changed left into an actual direction in the table
Okyloky9 #4
Posted 31 January 2013 - 10:11 AM
Well, bringing Misc Peripherals into this really stumps me, as I am not familiar with this mod. Sorry to have brought hopes up and down, but I don't know. Sorry I couldn't help, good luck finding an answer!
Engineer #5
Posted 31 January 2013 - 10:13 AM
Well, bringing Misc Peripherals into this really stumps me, as I am not familiar with this mod. Sorry to have brought hopes up and down, but I don't know. Sorry I couldn't help, good luck finding an answer!

The code I posted secondly, is not dependent on MiscPeripheral. It (hopefully, by this I am not sure if it is syntaxly correct) works fine without MiscPeripherals.
Orwell #6
Posted 31 January 2013 - 11:01 AM
What's your question exactly? I only see 4 lines of code with obscure comments after them. :P/> What do you mean with 'any possible string'? What do you want it to do? And what do you mean with 'made-up' values? And can you explain the format of the output from Misc-Peripherals to us? What are "north", "south", … used for? Do you need to specify them, or do you get a table with those keys, or …?

Also, you're example is syntactically incorrect, you can only use the []'s on tables using indices? In this case you could only use ["east"], ["west"], …
Engineer #7
Posted 31 January 2013 - 11:41 AM
What's your question exactly? I only see 4 lines of code with obscure comments after them. :P/> What do you mean with 'any possible string'? What do you want it to do? And what do you mean with 'made-up' values? And can you explain the format of the output from Misc-Peripherals to us? What are "north", "south", … used for? Do you need to specify them, or do you get a table with those keys, or …?

Also, you're example is syntactically incorrect, you can only use the []'s on tables using indices? In this case you could only use ["east"], ["west"], …

For the MiscPeripherals:
the interactive sorter has 6 outputs, those depend on the direction you are facing (north, south, east west)
I have this table setup, so the first number is right, then left, then forward and then back. Those can be called individually. My question was for that:
Can I use directions["north"] , or any other string for a direction. !! I derped hard on that post and edited it the right way !!

The actual questions of the topic:

With any possible string I mean that you can input a string for a variable (var1 = "IamAstringButYouKnowThisDefinitly") I still remember that you are the god here:P
With 'made-up' values I mean (this is also the main question) that I can set:

var4 = ImadeThisUp
var4 = anything
So its like you got a boolean (that is without strings if I remember correctly or true or false) but then made-up.

The question is than, is var4 possible? (I dont think so, but I want to be very sure. This can save me quite some lines if this is possible, and its handy for future scripts.)

I hope I made it clear, please let me know if you still dont understand me.

Many thanks,

Engineer
Kingdaro #8
Posted 31 January 2013 - 12:09 PM
Do you mean a nil value?
madeUpVar = nil

Or just
local madeUpVar
Orwell #9
Posted 31 January 2013 - 12:25 PM
Yeah, I still don't understand you. :P/> Can you give an example of how you would use these made-up variables in code? That could help.

Also, in the code you edited, you will get the table stored under that key when you do directions["north"].
So directions["north"] would then equal {5,4,2,3}. Is that what you want to achieve?
Engineer #10
Posted 31 January 2013 - 01:21 PM
I can then call directions["north"][1] right?

For the variables I am assuming that it is not possible since a lua god doesnt know what I mean. And thinking more and more about it it doesnt make any sence since you cant even call this.
Now I am embarrassed that made a topi and sort of solved it myself.


var4 = test -- note that this unstringed

I dont know what I was thinking when I made this topic, but I still learned something. Many thanks to all of you who tried to help me, it is very appreciated. Sorry for the useless topic.

Thanks,
Engineer
9GAG reference: trust me, im an engineer.
Orwell #11
Posted 31 January 2013 - 01:31 PM
Indeed, you're table example is perfectly correct. And being a Lua god on this forums means nothing more than that I post a lot. :P/>

I'm still curious. It's not that I don't know what you mean, that it's impossible. If you could just provide an example of how you'd like to use it, we could help. :)/>

What you do there with

var4 = test
Would make var4 equal the variable test. So if test is a string or number it will be a copy of test. If test is anything else (like table, function,…) then var4 will be the same object as test is. So changing var4 would change test.
If test hasn't been defined yet, it will be nil, so var4 will be nil as well.
ChunLing #12
Posted 31 January 2013 - 03:39 PM
If you're asking whether you can use an unassigned identifier as a variable in Lua, the answer is yes, but the value associated with such an identifier will always be nil.

So, in the first example:

var1 = true -- var1 will now "contain" the boolean value true
var2 = 1	 -- var2 will now "contain" the number value 1
var3 = "string" -- var3 will now "contain" the string value "string"
var4 = left   -- var4 will now "contain" the nil value nil, unless the identifier left was already associated with a non-nil value
This is perfectly valid, but rather pointless and potentially confusing. As Orwell points out, there are some differences in how different value types are passed, the simple types create a copy to associate with the new variable identifier, while the complex types only create a copy of the top reference (or pointer, if you will) and thus access the same memory structure as the copied variable.
Engineer #13
Posted 01 February 2013 - 07:38 AM
If you're asking whether you can use an unassigned identifier as a variable in Lua, the answer is yes, but the value associated with such an identifier will always be nil.

So, in the first example:

var1 = true -- var1 will now "contain" the boolean value true
var2 = 1	 -- var2 will now "contain" the number value 1
var3 = "string" -- var3 will now "contain" the string value "string"
var4 = left   -- var4 will now "contain" the nil value nil, unless the identifier left was already associated with a non-nil value
This is perfectly valid, but rather pointless and potentially confusing. As Orwell points out, there are some differences in how different value types are passed, the simple types create a copy to associate with the new variable identifier, while the complex types only create a copy of the top reference (or pointer, if you will) and thus access the same memory structure as the copied variable.

oooh.. Now I understand variables. Thank you very much ( everyone in this topic) for trying to help me. I guess I need to learn explain better in English. As you can see I live in the Netherlands and try my best to not come over stupidly. Lets get back to programming!:)/>
Orwell #14
Posted 01 February 2013 - 08:25 AM
As you can see I live in the Netherlands and try my best to not come over stupidly. Lets get back to programming! :)/>
You shouldn't try to. :)/> I'm from Belgium, so I have some difficulties with the English as well. Just go for it and explain very detailed what you want, good or bad English, and then someone will get it and help you out just fine. :D/>
Engineer #15
Posted 04 February 2013 - 01:51 AM
As you can see I live in the Netherlands and try my best to not come over stupidly. Lets get back to programming! :)/>
You shouldn't try to. :)/> I'm from Belgium, so I have some difficulties with the English as well. Just go for it and explain very detailed what you want, good or bad English, and then someone will get it and help you out just fine. :D/>
:D/>