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

Need some help with getting code to variably change itself

Started by Matrixmage, 07 August 2012 - 05:16 PM
Matrixmage #1
Posted 07 August 2012 - 07:16 PM
Hi, I recently got some help from MysticT and got a piece of code that "pings" the programs on a computer and prints them:

local programs = shell.programs()

for _, s in ipairs(programs) do
  print(s)
end
Now I'm trying to find out how to get the data about the programs found by this piece of code to be able to be implemented into another piece of code I have, so can I have some help?

Thanks in advance!

EDIT: Fixed my grammar ;)/>/>
Matrixmage #2
Posted 07 August 2012 - 08:57 PM
If anyone is confused by my grammar (English is my fist language, I was just rushing ;)/>/> ) I want the programs that are found by the above code to be able to be used by another piece of code (copying)
Cranium #3
Posted 07 August 2012 - 09:09 PM
What do you mean to get the data from the programs? Are you wanting to search for a specific program, and if it's there, use it in a block of code?
Matrixmage #4
Posted 07 August 2012 - 11:19 PM
What do you mean to get the data from the programs? Are you wanting to search for a specific program, and if it's there, use it in a block of code?

Sorry for any confusion, what I want is it to take all the programs it finds and use them in a certain block of code
Lyqyd #5
Posted 08 August 2012 - 01:15 AM
Well, the names of them are all in that table that you loop through to print them, so just create and use that table with your other code.
Pharap #6
Posted 08 August 2012 - 04:48 AM
All you need is the names of the programs that you want to be used.
Then in your code type:

shell.run("program's name goes here", "arguments here")
and it will run the programs you tell it. However, remember that this won't start all the programs at once, it will run them in the order you put them in.
If you want to run multiple programs at once, I think you can do it with coroutines. You'll have to ask someone else about those though, I've never been very good at multi-threading.
ElvishJerricco #7
Posted 08 August 2012 - 06:44 AM
What do you mean to get the data from the programs? Are you wanting to search for a specific program, and if it's there, use it in a block of code?

Sorry for any confusion, what I want is it to take all the programs it finds and use them in a certain block of code

There's a couple of ways you could do this. The simple way is to just use shell.run("programName"), which simply runs the program in a separate environment before letting your program continue. But there's a more complicated way as well.

Lua has a function called loadfile(). Pass in the path to a lua script file, and it returns a function to you. This returned function when run then runs the program, but also keeps it in context. I'll explain the benefit of that in a sec.


runProgram = loadfile("/path/to/my/program")
runProgram()

so as you can see, you're assigning the result of loadfile to a variable, that you call as a function in the next line. Calling the function is equivalent to running the program. But the program is then kept, as if it didn't close. So if you happen to know that that program declared a function called herpaDerp(), you can call that now (or just check if herpaDerp is nil and of type function and call it if it's not). It's practically like you took the code in that file and inserted it where you put runProgram().
KaoS #8
Posted 08 August 2012 - 08:02 AM
you have a spelling error in your code there ipairs() -> pairs()

@ElvishJerrico - nice dude, I didn't know that, it will be VERY useful so thanks
Luanub #9
Posted 08 August 2012 - 08:36 AM
You can use ipairs() or pairs(), both are valid options in a for loop.

http://www.lua.org/manual/5.1/manual.html#pdf-ipairs

http://www.lua.org/manual/5.1/manual.html#pdf-pairs
KaoS #10
Posted 08 August 2012 - 08:37 AM
hmm…. schooled again ;)/>/> you just never stop learning
Matrixmage #11
Posted 08 August 2012 - 06:18 PM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it ;)/>/> ).
Pharap #12
Posted 08 August 2012 - 06:49 PM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it ;)/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.
Matrixmage #13
Posted 08 August 2012 - 07:19 PM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it ;)/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

It's not a virus, its supposed to check the programs on a computer when it runs, and if any programs that are not on a list are found, it will overwrite them. I'm planning to use it on a tekkit server I have with my friends
Lyqyd #14
Posted 09 August 2012 - 01:50 AM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it ;)/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

What? Half the code is already in the first post. This would copy (or attempt to copy) a file overwriting every program listed by shell.programs():


local programs = shell.programs()
for _, path in ipairs(programs) do
 if not fs.isReadOnly then fs.copy("/file/to/copy", path) end
end
Pharap #15
Posted 09 August 2012 - 01:58 AM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it ;)/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

It's not a virus, its supposed to check the programs on a computer when it runs, and if any programs that are not on a list are found, it will overwrite them. I'm planning to use it on a tekkit server I have with my friends

Ah well, you should have just said a multiple program installer in the first place. From your first few posts it sounded like you were trying to overwrite things. Give me about 20 minutes and I'll return with a tested version of the program in a disk format so it will act just like an installer.
Pharap #16
Posted 09 August 2012 - 02:00 AM
Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

What? Half the code is already in the first post. This would copy (or attempt to copy) a file overwriting every program listed by shell.programs():


local programs = shell.programs()
for _, path in ipairs(programs) do
if not fs.isReadOnly then fs.copy("/file/to/copy", path) end
end

You would be surprised how many virus style posts I have come across. It's not uncommon.
Lyqyd #17
Posted 09 August 2012 - 02:08 AM
Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

What? Half the code is already in the first post. This would copy (or attempt to copy) a file overwriting every program listed by shell.programs():


local programs = shell.programs()
for _, path in ipairs(programs) do
if not fs.isReadOnly then fs.copy("/file/to/copy", path) end
end

You would be surprised how many virus style posts I have come across. It's not uncommon.

I was more pointing out that your middle paragraph was inaccurate (or you're not particularly familiar with the FS API?), not disagreeing with the amount of malicious scripting (which claim to be virii, but are not), and requests for help with same, that appears on the forum.
Matrixmage #18
Posted 09 August 2012 - 05:23 AM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it ;)/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

What? Half the code is already in the first post. This would copy (or attempt to copy) a file overwriting every program listed by shell.programs():


local programs = shell.programs()
for _, path in ipairs(programs) do
if not fs.isReadOnly then fs.copy("/file/to/copy", path) end
end

Thanks for the help, just have to adapt it to my code now, should be fun!
Matrixmage #19
Posted 09 August 2012 - 05:24 AM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it ;)/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

It's not a virus, its supposed to check the programs on a computer when it runs, and if any programs that are not on a list are found, it will overwrite them. I'm planning to use it on a tekkit server I have with my friends

Ah well, you should have just said a multiple program installer in the first place. From your first few posts it sounded like you were trying to overwrite things. Give me about 20 minutes and I'll return with a tested version of the program in a disk format so it will act just like an installer.

Sorry for the confusion, thanks for the help!
Pharap #20
Posted 09 August 2012 - 05:49 AM
Ok, so I got sidetracked and in the end didn't get round to making the program, but I have a good reason. I was working on my improved GPS system and have had a breakthrough.

And while I said it was quite a task, that was when I thought he was looking to seek out all the different files and emulate the list function. Testing if a program exists and if not, installing it is an easy task.
Luanub #21
Posted 09 August 2012 - 08:59 AM
hmm…. schooled again ;)/>/> you just never stop learning

That's what I love best about CC. It's even helped improve some of my scripting at work.
Matrixmage #22
Posted 14 August 2012 - 02:03 AM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it :P/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

What? Half the code is already in the first post. This would copy (or attempt to copy) a file overwriting every program listed by shell.programs():


local programs = shell.programs()
for _, path in ipairs(programs) do
if not fs.isReadOnly then fs.copy("/file/to/copy", path) end
end

I don't know if anyone is still watching this, but I'm having a brain-fart. The "fs.copy" on line 3, which of the two parts in the brackets do I replace with what data (where it goes etc.)?

Sorry for the inconvenience I just can't remember for the life of me. :D/>/>
Pharap #23
Posted 14 August 2012 - 02:19 AM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it :P/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

What? Half the code is already in the first post. This would copy (or attempt to copy) a file overwriting every program listed by shell.programs():


local programs = shell.programs()
for _, path in ipairs(programs) do
if not fs.isReadOnly then fs.copy("/file/to/copy", path) end
end

I don't know if anyone is still watching this, but I'm having a brain-fart. The "fs.copy" on line 3, which of the two parts in the brackets do I replace with what data (where it goes etc.)?

Sorry for the inconvenience I just can't remember for the life of me. :D/>/>

First argument is the current file, second is the destination path.
So to get a program called fun from a disk to the computer, you would put:


fs.copy("disk/fun", "fun")
Matrixmage #24
Posted 16 August 2012 - 07:31 AM
This is supposed to copy a program to all the programs found (I have my own code to specify certain programs) I don't think the code would do that, or would it?

Sorry for acting like a idiot I just haven't been able to wrap my head around coding this past week. :(/>/>
Matrixmage #25
Posted 18 August 2012 - 10:37 PM
Thanks for all the help, the only problem is that I don't know how to use tables, or that's what I would use. The purpose of my program is use the code I put in the first post to check all the programs on a computer and if it finds a certain one (already have that code) then it would copy another program onto the found program. Could someone point me at a basic tutorial for tables so I could utilize them? Or could could someone give me a universal "copy all" that would copy a given program name to all of the programs found by the code in the first post (I already have the code that only uses it for certain programs, and I want the challenge of adapting it :(/>/> ).

Sounds like you are trying to make a virus that overwrites all installed programs with a virus program.
If that's what you are trying to do, it's not going to work very well. You can only overwrite user-made programs, the pre-installed programs are access protected.

Also if you are new to lua, this is quite a step up. I'm fairly experienced with programming and something like this would take me a decent time to get right, so it's not an ideal project for a beginner.

What is the exact goal of this program? If it's supposed to be a virus that replaces every program in the computer with something malicious, you're unlikely to get any help. If it's simply an installer for a set of programs, that much I can do.

What? Half the code is already in the first post. This would copy (or attempt to copy) a file overwriting every program listed by shell.programs():


local programs = shell.programs()
for _, path in ipairs(programs) do
if not fs.isReadOnly then fs.copy("/file/to/copy", path) end
end

I don't know if anyone is still watching this, but I'm having a brain-fart. The "fs.copy" on line 3, which of the two parts in the brackets do I replace with what data (where it goes etc.)?

Sorry for the inconvenience I just can't remember for the life of me. :)/>/>

First argument is the current file, second is the destination path.
So to get a program called fun from a disk to the computer, you would put:


fs.copy("disk/fun", "fun")

Thank you so much! I've been trying to get this code working for ages!
Pharap #26
Posted 19 August 2012 - 12:53 PM
First argument is the current file, second is the destination path.
So to get a program called fun from a disk to the computer, you would put:


fs.copy("disk/fun", "fun")

Thank you so much! I've been trying to get this code working for ages!

Glad you've got it working at last. Hope your project goes well.