504 posts
Posted 04 February 2013 - 04:14 AM
Hello there,
I am having a program which saves and loads an array in a file.
My questions are now:
I save an one dimensional array with e.g. 10 variables set on '0'. Then i want to expand this array by 40 more variables. How do i do this without losing the old variables.
How are all the commands for the direction the file will be safed, so that a computer and turtle can access it.
Thank you for your help :D/>
UN0BTANIUM
2088 posts
Location
South Africa
Posted 04 February 2013 - 04:51 AM
Use the append mode
f = fs.open("file", "a")
to write to a file
keepin the old stuff. Using "w" will overwrite eveything.
If I understood you correctly?
504 posts
Posted 04 February 2013 - 05:11 AM
Yeah, thats kinda what i want.
And how do i expand existing arrays, so they can hold more variables than before.
and how to load and save from different folder directions?
2088 posts
Location
South Africa
Posted 04 February 2013 - 05:15 AM
So you want to insert variables into an existing table?
table.insert(tableName, "stuff")
To save into a different folder
directory, use the full path.
f = fs.open("myOS/tables/myFile", "a")
Take not: You can't write to rom
504 posts
Posted 04 February 2013 - 05:55 AM
table.insert() appends more variables?
And the starting folder ist rom?
edit:
What if i create a table at the beginning of the program and then load an array out of a file into this one. I dont want to append the table from the file. I just want want the variables from the file in the existing array.
1054 posts
Posted 04 February 2013 - 06:10 AM
table.insert() appends more variables?
And the starting folder ist rom?
edit:
What if i create a table at the beginning of the program and then load an array out of a file into this one. I dont want to append the table from the file. I just want want the variables from the file in the existing array.
Then make sure your table is empty by doing
tableName = {}
first. Then you can insert the elements from the file into it.
504 posts
Posted 04 February 2013 - 06:32 AM
But i dont want to have a empty array. The size of the array in the program and the array in the file are maybe different :/
Just you can follow for what i need this (just a simple version):
I make a logistic storage with chests. Below the turtle is a chest which contains items as indicator for thge number of all chests the storage has. The player can add chests manually and then has to change the amount of items in this chest. By starting the turtle, it sucks these items in its inventory and creates an array based on the item count. All variables are set on default, means that every chest in the storage is empty. Then it loads the array out of the file. This array might be smaller. And thats were i dont know how to get it to work :/
1054 posts
Posted 04 February 2013 - 07:08 AM
You're leaving out the relevant part. What are you going to do with the eventually smaller array? Do you mean that part of the counted items could already be stored into the file and only some will have to be added to it?
504 posts
Posted 04 February 2013 - 07:26 AM
The eventually smaller array should be implemented into the created array.
Maybe i have an idea how to do it but i want a solution from you.
Spoiler
I have an array like this allready saved in the file from a session before:
chest[1]=true
chest[2]=true
chest[3]=false
chest[4]=true
That means there are four items in the chest below the turtle, called the indicator items.
The setup includes 4 chests. The player added 2 more chests and placed 2 more indicator items in the chest below the turtle. The player restarts the program and the turtle creates with the indicator items an array.
chest[1]=false
chest[2]=false
chest[3]=false
chest[4]=false
chest[5]=false
chest[6]=false
Now the turtle loads the array from the file, which includes just four variables. The final array should look like this:
chest[1]=true
chest[2]=true
chest[3]=false
chest[4]=true
chest[5]=false
chest[6]=false
I hope this was clear enough what i want. Maybe there is an easier version of this and i am thinking too complicated :/
2088 posts
Location
South Africa
Posted 04 February 2013 - 08:03 AM
Try this?
chest = {}
chest[1]=true
chest[2]=true
chest[3]=false
chest[4]=true
f = fs.open("file", "w")
for i = 1, #chest do
f.writeLine(chest[i]) -- write to file
end
f.close()
chest = {}
chest[1]=false
chest[2]=false
chest[3]=false
chest[4]=false
chest[5]=false
chest[6]=false
f = fs.open("file", "r")
line = f.readLine()
i = 1
while line do
chest[i] = (line == "true") -- will make it true if the read line is true, else false.
i = i + 1
line = f.readLine()
end
f.close()
for i = 1, #chest do
print(chest[i])
end
504 posts
Posted 04 February 2013 - 08:10 AM
Yeah. Thats what i need. And if i want to use numbers and strings instead of boolean?
2088 posts
Location
South Africa
Posted 04 February 2013 - 08:28 AM
Ok then change the while line do loop to
while line do
chest[i] = (line == "true" or line == "false") and line == "true" or line -- checks to see if line is true or false, according to that it turns it into a boolean, else it just makes it line
i = i + 1
line = f.readLine()
end
504 posts
Posted 04 February 2013 - 08:34 AM
Thank you very much <3
504 posts
Posted 05 February 2013 - 08:47 PM
One last question!
If i want to open the same file from a turtle and a computer, do i just have to use something like "myOS/tables/myFile" and both have access to the same file?
2088 posts
Location
South Africa
Posted 05 February 2013 - 11:04 PM
No - computers and turtles have their own unique ID and own unique folder. One can not access either.
For this, rednet will be required.
7508 posts
Location
Australia
Posted 05 February 2013 - 11:13 PM
For this, rednet will be required.
Or the file on a disk in a drive that is directly in between the both.
Or in "/rom/".
2088 posts
Location
South Africa
Posted 05 February 2013 - 11:20 PM
For this, rednet will be required.
Or the file on a disk in a drive that is directly in between the both.
Or in "/rom/".
Well… If he's using a turtle, I doubt it's going to sit in one spot.Also, he's saving his own variables in the file, so he can't write it to rom.
7508 posts
Location
Australia
Posted 05 February 2013 - 11:27 PM
Also, he's saving his own variables in the file, so he can't write it to rom.
Core files to rom and a separate 'variables' file in the local system.
504 posts
Posted 05 February 2013 - 11:36 PM
I will use rednet ;D