Jump to content




Tables? (for making a settings file)


16 replies to this topic

#1 OrdinaryCassetteNerd

  • Members
  • 10 posts
  • LocationDonje Sitno (a village), Croatia

Posted 11 December 2017 - 06:15 PM

This one was making me go mad ever since I tried to make my own GUI OS. Basically, I need to create a whole table of settings that will fit into one file, but how? I have thought of writing each setting in each file, but not practical, right? The real question is, how do I fit all settings for my OS into one file? I will accept the JSON API, but I prefer the settings API as I find it much easier to use.

#2 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 11 December 2017 - 06:20 PM

I see you've mentioned the settings API, have you had a look at its documentation and implementation?

More specifically, One can use textutils.serialise and textutils.unserialise to convert from a table to a string and back again. One can serialise a table, write it to a file to save settings. Then read a file and deserialise it to load settings.

#3 OrdinaryCassetteNerd

  • Members
  • 10 posts
  • LocationDonje Sitno (a village), Croatia

Posted 11 December 2017 - 06:29 PM

Quote

More specifically, One can use textutils.serialise and textutils.unserialise to convert from a table to a string and back again. One can serialise a table, write it to a file to save settings. Then read a file and deserialise it to load settings.
OK, gonna try. Thanks!

P. S. How to even create a table?

Edited by OrdinaryCassetteNerd, 11 December 2017 - 06:30 PM.


#4 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 11 December 2017 - 06:36 PM

View PostOrdinaryCassetteNerd, on 11 December 2017 - 06:29 PM, said:

P. S. How to even create a table?
I'd suggest not starting work on an OS quite yet. Have a read through Programming in Lua (maybe the Lua manual as well if you're feeling brave) and try to build some simple programs first: door looks, turtle quarry, etc... Otherwise it's going to be like learning to swim by chucking someone into the ocean.

#5 Saldor010

  • Members
  • 467 posts
  • LocationThe United States

Posted 11 December 2017 - 07:08 PM

One thing I would suggest, if you're still interested in figuring out how to do this, might be to write an interpreter to read and write to .conf files. It'd be a good way to help introduce you to learning how to work with tables, as well as learning how to read and write from files and basic string manipulation.

#6 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 11 December 2017 - 07:45 PM

View PostSquidDev, on 11 December 2017 - 06:36 PM, said:

I'd suggest not starting work on an OS quite yet. Have a read through Programming in Lua (maybe the Lua manual as well if you're feeling brave) and try to build some simple programs first: door looks, turtle quarry, etc... Otherwise it's going to be like learning to swim by chucking someone into the ocean.

To build on what SquidDev said, and to help you along in whatever project you end up choosing to do, I also suggest you read Lupus590's tutorial on making complex goals manageable.

And to give you an idea of the can of worms you're opening with trying to make an OS, take some time and read through BB's Guide to Coroutines - unless you can understand this, your 'OS' isn't going to be much of an OS as all.

As SquidDev said, you should really focus on simpler projects to start, then work your way up to writing something as complex as an 'OS'.

Edited by Dog, 11 December 2017 - 07:46 PM.


#7 OrdinaryCassetteNerd

  • Members
  • 10 posts
  • LocationDonje Sitno (a village), Croatia

Posted 11 December 2017 - 08:12 PM

Yeah, yeah, yeah, but how do I actually choose an entry from the table and use its value? Same thing as with arguments "args[1]"?

#8 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 11 December 2017 - 08:22 PM

That depends on how you make the table in question, and syntactical preference. For example, term is a table. As is os. args is also a table, though IIRC it is deprecated.
https://www.lua.org/pil/2.5.html

Edited by KingofGamesYami, 11 December 2017 - 08:22 PM.


#9 OrdinaryCassetteNerd

  • Members
  • 10 posts
  • LocationDonje Sitno (a village), Croatia

Posted 12 December 2017 - 05:35 PM

View PostKingofGamesYami, on 11 December 2017 - 08:22 PM, said:

That depends on how you make the table in question, and syntactical preference. For example, term is a table. As is os. args is also a table, though IIRC it is deprecated. https://www.lua.org/pil/2.5.html
Then what's the non-deprecated equivalent to args?

#10 Saldor010

  • Members
  • 467 posts
  • LocationThe United States

Posted 12 December 2017 - 07:24 PM

View PostOrdinaryCassetteNerd, on 12 December 2017 - 05:35 PM, said:

Then what's the non-deprecated equivalent to args?

http://www.computerc..._World_Tutorial

#11 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 12 December 2017 - 08:26 PM

View PostOrdinaryCassetteNerd, on 12 December 2017 - 05:35 PM, said:

Then what's the non-deprecated equivalent to args?

Varargs, which are explained in this section of the manual.

Typically I will simply define tArgs:

local tArgs = {...}

...though in some cases I'll simply use them as-is, without a table.

#12 Lignum

  • Members
  • 558 posts

Posted 13 December 2017 - 12:48 AM

View PostKingofGamesYami, on 12 December 2017 - 08:26 PM, said:

...

local tArgs = {...}

...though in some cases I'll simply use them as-is, without a table.

It's odd to me how everyone wraps varargs in a table, and names it exactly "tArgs", nothing else, almost universally. Is this some sort of unwritten rule of CC? I find this syntax much more intuitive:
local arg1, arg2, arg3 = ...


#13 Dog

  • Members
  • 1,179 posts
  • LocationEarth orbit

Posted 13 December 2017 - 01:08 AM

View PostLignum, on 13 December 2017 - 12:48 AM, said:

It's odd to me how everyone wraps varargs in a table, and names it exactly "tArgs", nothing else, almost universally. Is this some sort of unwritten rule of CC? I find this syntax much more intuitive:
local arg1, arg2, arg3 = ...

If I'm not mistaken, tArgs is shorthand for 'table of arguments'.

I can't tell you why others use a table, but I can tell you why I do. While capturing each argument in a separate variable might be easier in some instances, what do you do when you don't know how many arguments you are going to get? For me, regardless of count, putting them in a table makes it easier to manage since each arg is a separate table entry and they're numbered sequentially (and I don't have to type out arg1, arg2, arg3, arg4, etc. = ...).

Edited by Dog, 13 December 2017 - 01:12 AM.


#14 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 13 December 2017 - 01:11 AM

Personally, I always make tArgs so I can verify that my program is getting the correct command line arguments. That or I'm using it inside a function which is designed to take (virtually) unlimited arguments, which would be impossible to represent using that syntax.

I'm pretty sure I picked up this habit from reading the source code.

Edited by KingofGamesYami, 13 December 2017 - 01:13 AM.


#15 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 13 December 2017 - 11:07 AM

View PostLignum, on 13 December 2017 - 12:48 AM, said:

It's odd to me how everyone wraps varargs in a table, and names it exactly "tArgs", nothing else, almost universally. Is this some sort of unwritten rule of CC?

It's more of a Lua rule than just CC, but I don't like hungerian notation and would use args as my variable name. Although, in languages where you don't delare types (like Lua), I see the usefulness of hungerian notation.

#16 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 13 December 2017 - 12:00 PM

View PostKingofGamesYami, on 12 December 2017 - 08:26 PM, said:

View PostOrdinaryCassetteNerd, on 12 December 2017 - 05:35 PM, said:

Then what's the non-deprecated equivalent to args?

Varargs, which are explained in this section of the manual.

Er, not exactly. The auto-built "args" table is a feature of vararg functions, not some sort of "alternative". There is no equivalent: after Lua 5.1, you're simply expected to build that table yourself, if you want it.

The feature's still available in ComputerCraft, but many coders don't use it for whatever reason. Probably because they don't realise it's there. It's handy, though, in that it automatically includes an "n" key telling you exactly how many arguments were passed to the function - you don't have to request a count-up later.

#17 SquidDev

    Frickin' laser beams | Resident Necromancer

  • Members
  • 1,427 posts
  • LocationDoes anyone put something serious here?

Posted 13 December 2017 - 12:03 PM

View PostBomb Bloke, on 13 December 2017 - 12:00 PM, said:

The feature's still available in ComputerCraft, but many coders don't use it for whatever reason. Probably because they don't realise it's there. It's handy, though, in that it automatically includes an "n" key telling you exactly how many arguments were passed to the function - you don't have to request a count-up later.
It's worth noting that it's removed in Lua 5.2. Whilst CC uses 5.1 (and likely will forever) I do like to code with that in mind. If you use table.pack(...), one will also get the n field - especially useful when you've got nil arguments.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users