This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Determing the amountof data in a Table
Started by ClassicRockFan, 14 February 2014 - 03:08 PMPosted 14 February 2014 - 04:08 PM
Hey guys, I'm trying to make a program that once all of the data in a table is printed(with an unknown number of data points), that it will go onto the next function. Is there some kind of function that will allow me to calculate how many data points are in the table? ( I'm running MC 1.5.2 ) Thanks for all of the help!
Posted 14 February 2014 - 04:12 PM
If you just want the surface table, a simple pairs for loop would suffice.
for k,v in pairs(table) do
print(v)
end
Why do you need to know how many "data points" are in the table though?Posted 14 February 2014 - 04:18 PM
- Basically, I'm trying to print a directory of all of the files on the computer(with one file being "selected"),and once it prints all of the files, to go onto another function which tells the computer to start waiting for a key to be pressed which changes the value of "selected", then reruns the first function and highlights a different file.
Edited on 14 February 2014 - 03:19 PM
Posted 14 February 2014 - 04:24 PM
http://pastebin.com/PMyPpm7p All of that is being used elsewhere in the final program, I just condensed it to serve the purpose of ease of reading. But that should work to demonstrate what its supposed to do. I forgot to tell the program to do file() and comment out the last bit of the function file() so it wont work. Oh well…
Edited on 14 February 2014 - 03:29 PM
Posted 14 February 2014 - 04:35 PM
Im kinda lost here… you already have all the files on the computer from calling fs.list, and you are already counting all the file entries in the table it returns with the counter variable. What more do you need?
Posted 14 February 2014 - 04:46 PM
It's constantly adding 1 to the counter, I need to have it so that when all of the data is printed, then it will break the for loop and move onto fileWait(). The counter is only used to set the background color.
Posted 14 February 2014 - 05:01 PM
wat? The for loop ends on it's own when there is no more table entries.
just change this
just change this
if FileList[counter] then
fileWait()
end
end
end
to this
end -- closes for loop
fileWait()
end
Edited on 14 February 2014 - 04:01 PM
Posted 14 February 2014 - 05:49 PM
As pasted, we've got a file() function which calls fileWait(), and a fileWait() function that calls file(). What we don't have is any other calls to these functions, so neither gets the initial start they need for the script to actually do anything. Adding a line reading just "fileWait()" to the bottom of the script should trigger some action (yes, you'll want to incorporate Comet's fix too).
Though even with all that in place, the recursive calls (functions calling functions calling functions etc with none ever finishing) will result in a crash after moving up/down the list of files a hundred times or so. Maybe make that a "while true do file() end" on the last line, remove the "while" loop from fileWait(), and also remove all the "file()" lines from that, too.
If for whatever reason you need to straight-up know how many entries are in a numerically indexed table such as FileList, there are a few ways - eg just check the value of #FileList.
Though even with all that in place, the recursive calls (functions calling functions calling functions etc with none ever finishing) will result in a crash after moving up/down the list of files a hundred times or so. Maybe make that a "while true do file() end" on the last line, remove the "while" loop from fileWait(), and also remove all the "file()" lines from that, too.
If for whatever reason you need to straight-up know how many entries are in a numerically indexed table such as FileList, there are a few ways - eg just check the value of #FileList.
Edited on 14 February 2014 - 04:51 PM
Posted 14 February 2014 - 05:56 PM
The art of reading is lost to some i see…
http://pastebin.com/PMyPpm7p All of that is being used elsewhere in the final program, I just condensed it to serve the purpose of ease of reading. But that should work to demonstrate what its supposed to do. I forgot to tell the program to do file() and comment out the last bit of the function file() so it wont work. Oh well…
Posted 14 February 2014 - 06:10 PM
Fair 'nuff. What I mentioned about the recursive calls stands, though - I still recommend incorporating a "while" loop instead.
Posted 14 February 2014 - 06:52 PM
Okay, it was a derp on my part. I was reseting the variable after I selected a new one so it would never have worked.
Posted 15 February 2014 - 05:45 PM
Okay. I will make the edits mentioned. But I have another question. When I run the progrAm, it likes to keep going past the bottom of the list. Is there a way thank you can think if ither than the one I have to fix this problem? And comet wolf, I appreciate the help but don't appreciate the snide remarks. I'm not a total retard and just wanted an answer, and I'm sorry I overlooked something so basic.
Posted 15 February 2014 - 10:20 PM
The big bad wolf…
What do you mean by "going past the bottom of the list"? Like it adds non-existant files to the list, or what?
What do you mean by "going past the bottom of the list"? Like it adds non-existant files to the list, or what?
Posted 15 February 2014 - 11:14 PM
you can get the number of indices of the table with
local infoTab = {} --insert whatever here...
local indexNum = #infoTab
Posted 16 February 2014 - 12:02 PM
When I scroll down, the value of "selected" is going past the number of files. So it appears as though none are "selected".
Posted 16 February 2014 - 06:15 PM
The reason for this is that the "counter" which contains the length of "FileList" is local to the "file()" function. This means the "fileWait()" function can't see it.
The quick'n'dirty fix is to move the initial declarations of counter/selected up and outside of any functions.
The quick'n'dirty fix is to move the initial declarations of counter/selected up and outside of any functions.
Posted 16 February 2014 - 10:31 PM
Okay. Thanks. I didn't know that variables declared in a function were local for that function. Good to note. I'll redefine the variable Thanks for all of the help.