This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Imprivate's profile picture

Read 'list' program?

Started by Imprivate, 28 December 2013 - 09:28 PM
Imprivate #1
Posted 28 December 2013 - 10:28 PM
Hello,

I've made this:


mylist = shell.run("list")
print("Type the name of the file to view.")
answer = read()

What I need to do is check if 'answer' contains at least one program from 'list'. Is there any way to do this?
Edited on 28 December 2013 - 09:29 PM
OReezy #2
Posted 28 December 2013 - 11:52 PM
I'm not sure what you are trying to do, but this is how I usually check an input against a list of valid options. Hope this helps.

local table = {"input 1", "input 2", "etc"}

local userVar = read()
for w in pairs(table) do
  if table[w] == userVar then
	break
  end
end
Edited on 28 December 2013 - 10:52 PM
Lyqyd #3
Posted 28 December 2013 - 11:57 PM
Use fs.list() to get the list of files, just like the list program does. Why didn't you look at what the list program actually does?
Imprivate #4
Posted 29 December 2013 - 06:19 AM
Thanks for your answers, heres what I did:


local answer = read()
local mytable = {"fs.list()"}
for w in pairs(mytable) do
  if mytable[w] == answer then
	print("worked")
  else
	print("didn't work")
  end
end

This just prints "didn't work" every time, I'm guessing because its checking if I i'm writing the entire 'list' program. Any help on checking if it contains at least one thing from the list?

P.S: I'm new to programming, sorry if I sound newbie.
Edited on 29 December 2013 - 05:21 AM
LBPHacker #5
Posted 29 December 2013 - 07:21 AM
-snip-
Almost. fs.list returns the table itself, so all you have to do is:
local mytable = fs.list()

For the record, I'll explain what you did up there:
local mytable = {"fs.list()"}
  • You created a string "fs.list()"
  • You created a new table
  • You put that string into the table
  • You did all this in the table constructor which assigned the first index of the table to that string
  • You can extract that string from the table like this:
print(mytable[1]) -- prints "fs.list()" without the quotes
  • Finally, you assigned mytable to your newly created table
Edited on 29 December 2013 - 06:22 AM
Imprivate #6
Posted 29 December 2013 - 09:38 AM
LBPHacker - thanks so much for the explanation, I get it now. However when I run the program using
local mytable = fs.list()
Whatever I type I still get the error: "MyApplication:5: Expected string" (line 5 is: local mytable = fs.list() ). Any idea why? Full code below:


sleep(0.5)

local answer = read()
local mytable = fs.list()
for w in pairs(mytable) do
  if mytable[w] == answer then
	print("worked")
  else
	print("didn't work")
  end
end
Edited on 29 December 2013 - 08:39 AM
wieselkatze #7
Posted 29 December 2013 - 10:18 AM
the fs.list() should be fs.list("/") or the path you want to be listed
Imprivate #8
Posted 29 December 2013 - 01:41 PM
This works, thank you so much everyone :)/>
LBPHacker #9
Posted 29 December 2013 - 02:28 PM
Yeah, my bad. Shoulda mentioned that fs.list needs a path to list things in…
Rodzyn #10
Posted 30 December 2013 - 04:41 PM
You propably want it to be something more like this:

local answer = read()
local mytable = fs.list("/path/to/be/checked")
local message = "Didn't work" -- Variable containing message to be printed at the end of check

for w in pairs(mytable) do
	if mytable[w] == answer then
	   message = "Worked" -- Found a match, now the message is set to "Worked"
	   break -- We found what we're looking for, no need to go through rest of the table
	end
end

print(message)
It will initially set message to "Didn't work", go through table with list of files. If it finds matching position it wil set message to "Worked" and stop looking since it found what we're looking for. If it'll go through whole table and won't find match it will print "Didn't work".
aaa #11
Posted 30 December 2013 - 06:16 PM
The default programs are in /rom/programs, in /rom/programs/computer and /rom/programs/turtle (only one of the two last, games are designed for computers, and excavate works only for turtles).

When you discover a new function, (here fs.list), take a look on the wiki, there is a brief documentation with an example which shows how to use it.

It can be a bit hard if you are very very new, but you will learn a lot by reading default programs to know how they work, and it is quite interssing.

Last but not least : you have to distinguish functions and programs : functions can return results and be used in the code of an other function/program as programs just run on there own (here, list print stuff but does not return any actual list to be used)
Imprivate #12
Posted 18 January 2014 - 01:54 PM
Sorry to revive this post from the dead, but I'm coming at this from a new angle. I'm creating a 'cleaning' program, where you enter the names of programs to delete and it will delete them, so I don't have to manually delete all my one-line tests that accumulate over time. More on the 'enter names of programs to be deleted' later, right now I'm just experimenting with deleting files in a certain directory. Here is what I have so far:

local mytable = fs.list("/JunkDirectory")
local i = 0
for w in pairs(mytable) do
  print("Deleted file: "..mytable[w])
  fs.delete(mytable[w])
  i = i+1
  sleep(0.1)
end
print(message)
It currently lists the names of everything in the directory , however the file is still there afterwards. Anybody got ANY idea why?

Pastebin code: VzVftGZC (you need to make a directory called 'JunkDirectory', then create a few files in there). Any help at all would be greatly appreciated. Thanks, Imprivate :)/>


P.S. I know this isn't the most useful tool, I'm just experimenting with different things in CC as to better my knowledge ^_^/>
OReezy #13
Posted 18 January 2014 - 02:07 PM
The problem is you are trying to delete an entry using the stored information(or value) when you need to be using the key. This is what a table looks like if you don't specify any keys
local table = {[1]="first entry",[2]="second entry",[3]="third entry"}
The generic for loop I gave you only returns a value, so when you try and delete an entry using w, you are doing this
fs.delete(table["first entry"])
This is nil because there is no table["first entry"]. To fix this use
for k,v in ipairs(table) do
  print(v)      -->prints the name of the file
  print(k)      -->prints the index of the file or the key
  print(table[k]) -->prints the name of the file by looking it up in the table with the key
end
If you have any more questions feel free to ask. And you really should have made a new thread.
Edited on 18 January 2014 - 01:09 PM
Imprivate #14
Posted 18 January 2014 - 03:20 PM
This is a carry-on of an old thread (thanks for the heads-up OReezy). I'm creating a 'cleaning' program where you enter the names of programs to delete and it will delete them, so I do not have to manually delete all the programs I don't use. I'm focusing on deleting now, so I will add the 'programs not to delete' feature later. My problem now is as follows:

local mytable = fs.list("/JunkDirectory")
local i = 0
for k,v in ipairs(mytable) do
print(v)
  fs.delete(v)
end

I still have the same problem (it lists all the programs, but doesn't delete them), it's just now just half as many lines of code. I'm probably doing something extremely simple wrong, as I'm new to tables and they confuse me 0.o. Anybody know what I'm doing wrong? Thanks in advance, Imprivate.

EDIT: I have realised the problem is that it's trying to delete the entire table at once, however I still do not know how I could make it delete each file in the table individually. Any ideas?

Pastebin code: dmZzLQq2 (create a directory called 'JunkDirectory', then create a few files in there, but run the code above in the normal place :)/>)
Edited on 18 January 2014 - 02:31 PM
wieselkatze #15
Posted 18 January 2014 - 03:25 PM
Your problem is the deleting (pretty obvious, hm? :P/>).
No, fs.list() just lists the files in /JunkDirectory, so for example "test" insted of "/JunkDirectory/test", so just put there fs.delete("/JunkDirectory/"..v)
Imprivate #16
Posted 18 January 2014 - 03:33 PM
@wieselkatze thank you so much, it works :D/> Never knew you could use variables in directory names. Thanks all for your help!
wieselkatze #17
Posted 18 January 2014 - 03:54 PM
I mean, why not? It's just a normal string ;)/>
Lyqyd #18
Posted 18 January 2014 - 06:03 PM
OReezy, you're not a mod. If someone has a further question about a piece of code, they should ask in the same topic they've been asking questions about that code in. Threads merged.