Jump to content




list of strings?


34 replies to this topic

#1 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 12:46 AM

I am making a program that will allow you to add, remove, and list multiple strings in order. Should i, with all things considered, use a document to store the strings, or an array?

#2 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 01:12 AM

Also, while i'm here, how do you check if a line of code ran?

#3 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 29 March 2015 - 01:42 AM

I'm a little confused at what you're asking, but here's what I've got:
You want to store data, using a table, but don't know how, and thought of using fs to do it.
-fs is for saving things permanently. It's good not good to use it to store temporary variables.

You are trying to debug your code

First of all, here's a tutorial on tables

Second of all, to check if a line of code ran, the best you can do is:
myFunction()
print("Ran.")

Edited by Geforce Fan, 29 March 2015 - 01:44 AM.


#4 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 01:56 AM

I am going to store the strings permanently. Context: I plan to be using a lot of Ender Chests and Ender Tanks from Ender Storage, and i want a way to keep track of what color combonations i've used and which i haven't. To do this, i wanted to make a program that would have this syntax:
Enderchest <list>/<add>/<remove>
list would list out all of the color combinations i have, add would allow me to add a used combination, and remove would allow me to remove a combination, and i would want my combinations stored such that they can be backed up via floppy disk.

Also, for checking if a line of code ran, i didn't mean debugging, i want to return the value of a print() command, and then have
if printWasTrueOrFalse == true then
print("true")
else
print("false")

Edited by YUGATUG, 29 March 2015 - 01:57 AM.


#5 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 29 March 2015 - 02:59 AM

You should use a table AND fs. To save a table to fs, just do
file.write(textutils.serialize(table))

To get that file, open it, and do
table = textutils.unserialize(file.readAll())

Why do you need to return the value of a print command?

Edited by Geforce Fan, 29 March 2015 - 02:59 AM.


#6 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 29 March 2015 - 05:27 AM

View PostGeforce Fan, on 29 March 2015 - 02:59 AM, said:

table = textutils.unserialize(file.readAll())

Don't do that EXACTLY, you'd override the table API ;)

View PostYUGATUG, on 29 March 2015 - 01:56 AM, said:

Also, for checking if a line of code ran, i didn't mean debugging, i want to return the value of a print() command, and then have

Well, you can check if a function errors with pcall:

local state,err = pcall(print,"hi")

If the function does error, state will be false and err will be the error. Otherwise, state will be true and err will be nil

You shouldn't need to check individual functions, just put your code into one huge function, then pcall that

#7 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 03:55 PM

I see, thanks! I need to check if a line of code ran because if 'Enderchest list' prints nothing i want the program to print("ERROR: No Entries")

#8 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 29 March 2015 - 04:14 PM

The program which would print the error would be a different program than the 'Enderchest' program, right? If that's the case, you should probably make an API for listing, adding and removing entries.

#9 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 04:19 PM

In reply to HPWebcamAble:
If i don't do that, what should i do? I'd of course change the variable name, but if that command overwrites the table API, what wouldn't?

#10 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 29 March 2015 - 05:32 PM

View PostYUGATUG, on 29 March 2015 - 03:55 PM, said:

I need to check if a line of code ran because if 'Enderchest list' prints nothing i want the program to print("ERROR: No Entries")

Instead of looking at what 'print' returns, you could check how many entries are in the table that stores the colors, and if there aren't any then do the print("Error: No entries")

View PostYUGATUG, on 29 March 2015 - 04:19 PM, said:

In reply to HPWebcamAble:
If i don't do that, what should i do? I'd of course change the variable name, but if that command overwrites the table API, what wouldn't?

Just don't use 'table' as a variable and you'll be fine

#11 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 06:02 PM

Oh, I wouldn't of thought of that solution, thanks for the tips!
Btw i subbed to your channel :)

#12 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 29 March 2015 - 07:44 PM

View PostYUGATUG, on 29 March 2015 - 06:02 PM, said:

Btw i subbed to your channel :)

:D

#13 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 07:47 PM

View PostGeforce Fan, on 29 March 2015 - 02:59 AM, said:

You should use a table AND fs. To save a table to fs, just do
file.write(textutils.serialize(table))

To get that file, open it, and do
table = textutils.unserialize(file.readAll())

Why do you need to return the value of a print command?

I made a program called test:

table = {"yolo"}
file.write(textutils.serialize(table))
table = textutils.unserialize(file.readAll())

then i got this error: "test:2: attempt to index? (a nil value)"
then if i try to run ANY command in the shell it gives me this error: "list:21: attempt to call nil" and i have to reboot to use the computer again.

Edited by YUGATUG, 29 March 2015 - 07:48 PM.


#14 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 29 March 2015 - 07:51 PM

First of all, don't name your tables 'table', it overwrites an API, which causes your computer to break.

Okay, I asumed basic knowledge on fs, sorry.
Here's what you should do
_table = {"yolo"}--#Define global '_table' as {"yolo"}
file = fs.open("Insertfilenamehere","w")--#change Insertfilenamehere to your filename, "w" means that it's being opened in write mode
file.write(textutils.serialize(_table))--#Write the table to the file
file.close()--#Close the file. THIS IS ABSOLUTELY NECESSARY!

To open:
file = fs.open("Insertfilenamehere","r") --#change Insertfilenamehere to your filename, "r" means that it's being opened in read mode
_table = textutils.unserialize(file.readAll()) --#Assign global '_table' to the table in that file
file.close()--#Close the file. THIS IS ABSOLUTELY NECESSARY!

Edited by Geforce Fan, 29 March 2015 - 07:55 PM.


#15 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 08:01 PM

Ok, a file with yolo was created, but now how do i get it to print? I've been playing around and i can't figure it out, sorry if i'm missing something really obvious...

#16 KingofGamesYami

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

Posted 29 March 2015 - 08:09 PM

print( textutils.serialize( _table ) )

Just use this after loading the table from the file and it should work fine.

#17 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 10:17 PM

But that print's out the raw version, as in:
{
"yolo",
}
instead of:
yolo

#18 KingofGamesYami

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

Posted 29 March 2015 - 10:19 PM

Well, if you want just the first variable, you could use
print( _table[ 1 ] )


#19 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 11:10 PM

pastebin.com/46eS3z7U
No matter what i put after test, the program still outputs
{
"yolo",
}
even though the yolo in enderList is changed to whatever i put after test.

View PostKingofGamesYami, on 29 March 2015 - 10:19 PM, said:

Well, if you want just the first variable, you could use
print( _table[ 1 ] )
In this case i only have one variable, but when i actually write the code ill have 10 or more.

#20 Bomb Bloke

    Hobbyist Coder

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

Posted 29 March 2015 - 11:35 PM

View PostYUGATUG, on 29 March 2015 - 11:10 PM, said:

even though the yolo in enderList is changed to whatever i put after test.

No, you're changing "enderay", not "enderList". Remember that global variables you define don't have their contents wiped until the whole computer reboots.

View PostYUGATUG, on 29 March 2015 - 11:10 PM, said:

In this case i only have one variable, but when i actually write the code ill have 10 or more.

So use _table[1], _table[2], etc... or iterate through them... or use specific key-names... whatever. Here, have another tutorial.

Edited by Bomb Bloke, 29 March 2015 - 11:36 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users