This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
list of strings?
Started by YUGATUG, 28 March 2015 - 11:46 PMPosted 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?
Posted 29 March 2015 - 02:12 AM
Also, while i'm here, how do you check if a line of code ran?
Posted 29 March 2015 - 02: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:
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 on 29 March 2015 - 12:44 AM
Posted 29 March 2015 - 02: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")
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 on 29 March 2015 - 12:57 AM
Posted 29 March 2015 - 03: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?
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
Posted 29 March 2015 - 06:27 AM
table = textutils.unserialize(file.readAll())
Don't do that EXACTLY, you'd override the table API ;)/>
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
Posted 29 March 2015 - 04: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")
Posted 29 March 2015 - 05: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.
Posted 29 March 2015 - 05: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?
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?
Posted 29 March 2015 - 06:32 PM
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")
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
Posted 29 March 2015 - 07:02 PM
Oh, I wouldn't of thought of that solution, thanks for the tips!
Btw i subbed to your channel :)/>
Btw i subbed to your channel :)/>
Posted 29 March 2015 - 08:44 PM
Btw i subbed to your channel :)/>
:D/>
Posted 29 March 2015 - 08:47 PM
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 on 29 March 2015 - 06:48 PM
Posted 29 March 2015 - 08: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
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 on 29 March 2015 - 06:55 PM
Posted 29 March 2015 - 09: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…
Posted 29 March 2015 - 09:09 PM
print( textutils.serialize( _table ) )
Just use this after loading the table from the file and it should work fine.
Posted 29 March 2015 - 11:17 PM
But that print's out the raw version, as in:
{
"yolo",
}
instead of:
yolo
{
"yolo",
}
instead of:
yolo
Posted 29 March 2015 - 11:19 PM
Well, if you want just the first variable, you could use
print( _table[ 1 ] )
Posted 30 March 2015 - 12:10 AM
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.
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.
In this case i only have one variable, but when i actually write the code ill have 10 or more.Well, if you want just the first variable, you could useprint( _table[ 1 ] )
Posted 30 March 2015 - 12:35 AM
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.
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 on 29 March 2015 - 10:36 PM
Posted 30 March 2015 - 12:49 AM
line 3 is opening the document, line 4 is transferring the data from enderay to enderList, and then i'm not actually sure what line 8 does, but line 10 prints out enderList. unless something funky is happening in line 8, i don't see how i'm changing only enderay and not enderList. Please explain?
Posted 30 March 2015 - 01:48 AM
Your "enderList" file is not the same thing as your "enderList" variable.
Posted 30 March 2015 - 08:45 PM
Your "enderList" file is not the same thing as your "enderList" variable.
Enderlist is only said in line 3, 7, and 10. in line 3 it's used as file.fsopen, in line 7 it's used as file.fsopen again, then line 10 is print(textutils.serialize(enderList). Nowhere in the code can i find where Enderlist is created as a variable, unless line 10 creates a variable. If it does, what can i do to fix that?
Edited on 30 March 2015 - 08:06 PM
Posted 30 March 2015 - 10:38 PM
Line 10 is the one referring to enderList as a variable. You're trying to textutils.serialise() its contents, however, the actual table you want to print out is referred to as enderay.
Posted 30 March 2015 - 11:39 PM
I don't want to print out the variable… I want to print out the document (minus the curly brackets and quotations) because this is just a proof of concept for myself. Later, i will have more than just one string to print out, and i will need those strings to be backed up via floppy disk, which is why i decided to store my strings in a text document.
Posted 31 March 2015 - 07:07 AM
You can't print out the contents of your document without first loading it into a variable of some description. Consider that computers can't run programs without first copying them from your HDD into your RAM - think of variables as being like files stored in RAM. They're where you store your data when you actually want to do something with it.
Geforce Fan's example served to show you how you might take a table - which is basically an organised collection of variables, ideal for storing eg multiple lines of text (and many other things!) - and write it to your HDD, and he followed that with an example of how you would then load the content of the resulting file back into a table in RAM. They were two separate examples of different tasks. Doing them one after the other doesn't allow you to magically "print your file".
Perhaps try reading through this tutorial directory, top to bottom. By the time you've covered the one about scope (no, I'm not saying skip ahead to that one - go top to bottom!), you should have the info you need to start coding your project.
Geforce Fan's example served to show you how you might take a table - which is basically an organised collection of variables, ideal for storing eg multiple lines of text (and many other things!) - and write it to your HDD, and he followed that with an example of how you would then load the content of the resulting file back into a table in RAM. They were two separate examples of different tasks. Doing them one after the other doesn't allow you to magically "print your file".
Perhaps try reading through this tutorial directory, top to bottom. By the time you've covered the one about scope (no, I'm not saying skip ahead to that one - go top to bottom!), you should have the info you need to start coding your project.
Posted 31 March 2015 - 08:20 PM
Well, I'm probably going to skip to scope anyway, but if I have questions I will refer back to the tutorials first. Thanks!
Posted 01 April 2015 - 12:27 AM
You can't print out the contents of your document without first loading it into a variable of some description. Consider that computers can't run programs without first copying them from your HDD into your RAM - think of variables as being like files stored in RAM. They're where you store your data when you actually want to do something with it.
Geforce Fan's example served to show you how you might take a table - which is basically an organised collection of variables, ideal for storing eg multiple lines of text (and many other things!) - and write it to your HDD, and he followed that with an example of how you would then load the content of the resulting file back into a table in RAM. They were two separate examples of different tasks. Doing them one after the other doesn't allow you to magically "print your file".
Perhaps try reading through this tutorial directory, top to bottom. By the time you've covered the one about scope (no, I'm not saying skip ahead to that one - go top to bottom!), you should have the info you need to start coding your project.
I read the tutorials, they haven't helped with documents, unless I'm REALLY ignorant and missed it 3 times in a row.
Posted 01 April 2015 - 05:04 AM
With any luck, your new knowledge of variables, functions, loops and tables will allow you to understand the examples already provided to you within this thread, and give you some ideas as to how to go about writing… whatever it is that you're thinking of writing.
Let's recap, and perhaps expand a bit. Say you have a bunch of lines, stored in a table for convenient access:
Now let's say you want to write that to disk. We might use a for loop:
And if you wanted to read it back at a later time (instead of manually defining your document within your script's code, like with the table declaration above), you're again using fs.open() to get the file handle you're going to work with, but this time you're switching to read mode:
And again, to actually write all the lines in your table to the screen, you're looping:
It's quite possible to store your entire document within a single string, though the table solution makes it a lot easier to keep track of things. For example, if you wanted to insert a line in-between lines 2 and 3, you'd simply do:
Let's recap, and perhaps expand a bit. Say you have a bunch of lines, stored in a table for convenient access:
myDocument = {
"This is some text.",
"There are multiples lines of it.",
"",
"It's a fascinating read."
}
Now let's say you want to write that to disk. We might use a for loop:
local myFile = fs.open("myFile.txt", "w")
for i = 1, #myDocument do
myFile.writeLine(myDocument[i])
end
myFile.close()
And if you wanted to read it back at a later time (instead of manually defining your document within your script's code, like with the table declaration above), you're again using fs.open() to get the file handle you're going to work with, but this time you're switching to read mode:
local myFile, myDocument = fs.open("myFile.txt", "r"), {}
for line in myFile.readLine do
table.insert(myDocument, line)
end
myFile.close()
And again, to actually write all the lines in your table to the screen, you're looping:
for i = 1, #myDocument do
print(myDocument[i])
end
It's quite possible to store your entire document within a single string, though the table solution makes it a lot easier to keep track of things. For example, if you wanted to insert a line in-between lines 2 and 3, you'd simply do:
table.insert(myDocument, 3, "This is an inserted line.")
Posted 03 April 2015 - 11:55 PM
The way i will be adding strings is with this syntax: 'Enderchest add <string>', which allows for only one string. In that case, i don't need an array to store my strings (as well as all the for loops to cycle through strings to store in myFile), unless an array is needed for reading/writing the document. Also, you typed this (not using the quote feature because i don't know how to get it to quote only parts of a post):
local myFile, myDocument = fs.open("myFile.txt", "r"), {}
for line in myFile.readLine do
table.insert(myDocument, line)
end
myFile.close()
without defining 'line', wouldn't this return an error?
pastebin.com/FVXWeY3D
i typed what you typed, but got an error on line 6. Help?
local myFile, myDocument = fs.open("myFile.txt", "r"), {}
for line in myFile.readLine do
table.insert(myDocument, line)
end
myFile.close()
without defining 'line', wouldn't this return an error?
pastebin.com/FVXWeY3D
i typed what you typed, but got an error on line 6. Help?
Edited on 03 April 2015 - 09:56 PM
Posted 03 April 2015 - 11:59 PM
You forgot the for loop and to pass the line variable to table.insert.
Edited on 03 April 2015 - 09:59 PM
Posted 04 April 2015 - 12:23 AM
i typed what you typed, but got an error on line 6. Help?
Not quite, you didn't - spot the missing comma.
Posted 04 April 2015 - 04:17 AM
My bad, i put in the comma, but the program prints a blank line, as in the contents of "". Also, why did you make me have to find where to put the comma, instead of telling me to put a comma after the end parenthesis after line 6?
Posted 04 April 2015 - 05:47 AM
Because telling you what the problem is but not where to find it helps you to learn and understand the code, whereas spoon-feeding you the solution does not. We're here to help you learn.
Posted 04 April 2015 - 02:11 PM
The 5 second adventure of comparing multiple lines of text to similar lines of text did not help me learn or understand the code
But line was never defined…
Also, i will add the for loop when i make the actual program, right now i want to figure out how to use fs.
You forgot the for loop and to pass the line variable to table.insert.
But line was never defined…
Also, i will add the for loop when i make the actual program, right now i want to figure out how to use fs.
Edited on 04 April 2015 - 10:58 PM