Jump to content




list of strings?


34 replies to this topic

#21 YUGATUG

  • Members
  • 56 posts

Posted 29 March 2015 - 11:49 PM

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?

#22 Bomb Bloke

    Hobbyist Coder

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

Posted 30 March 2015 - 12:48 AM

Your "enderList" file is not the same thing as your "enderList" variable.

#23 YUGATUG

  • Members
  • 56 posts

Posted 30 March 2015 - 07:45 PM

View PostBomb Bloke, on 30 March 2015 - 12:48 AM, said:

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 by YUGATUG, 30 March 2015 - 09:06 PM.


#24 Bomb Bloke

    Hobbyist Coder

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

Posted 30 March 2015 - 09: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.

#25 YUGATUG

  • Members
  • 56 posts

Posted 30 March 2015 - 10: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.

#26 Bomb Bloke

    Hobbyist Coder

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

Posted 31 March 2015 - 06: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.

#27 YUGATUG

  • Members
  • 56 posts

Posted 31 March 2015 - 07: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!

#28 YUGATUG

  • Members
  • 56 posts

Posted 31 March 2015 - 11:27 PM

View PostBomb Bloke, on 31 March 2015 - 06:07 AM, said:

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.

#29 Bomb Bloke

    Hobbyist Coder

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

Posted 01 April 2015 - 04: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:

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.")


#30 YUGATUG

  • Members
  • 56 posts

Posted 03 April 2015 - 10: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?

Edited by YUGATUG, 03 April 2015 - 10:56 PM.


#31 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 03 April 2015 - 10:59 PM

You forgot the for loop and to pass the line variable to table.insert.

Edited by MKlegoman357, 03 April 2015 - 10:59 PM.


#32 Bomb Bloke

    Hobbyist Coder

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

Posted 03 April 2015 - 11:23 PM

View PostYUGATUG, on 03 April 2015 - 10:55 PM, said:

i typed what you typed, but got an error on line 6. Help?

Not quite, you didn't - spot the missing comma.

#33 YUGATUG

  • Members
  • 56 posts

Posted 04 April 2015 - 03: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?

#34 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 04 April 2015 - 04: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.

#35 YUGATUG

  • Members
  • 56 posts

Posted 04 April 2015 - 01: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

View PostMKlegoman357, on 03 April 2015 - 10:59 PM, said:

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 by YUGATUG, 04 April 2015 - 11:58 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users