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

string.find

Started by BlueZero, 16 June 2013 - 07:05 AM
BlueZero #1
Posted 16 June 2013 - 09:05 AM
Can't seem to find what's located in another file. It just skips over to the else. It worked before till I started doing some pathing with it, now I think I gotta adjust it but I'm not sure how. Oh and as for the naming… Don't ask. :D/>


if fs.exists("derp") then
				local rf = fs.open("derp", "r")
				lines = rf.readAll()  
				rf.close()
				while lines do
						if lines and string.find(lines, [[shell.run%("herpherp-1.0/herpherp"%)]]) then
								print("FOUND IT!")					
						else
								print("Didn't find herpherp.")
								error()
						end
				end
end

Hypothetically speaking this is what I've got. Within the file, "derp" will have "shell.run("herpherp-1.0/herpherp"). Before when it was just shell.run("herpherp") it would work with the setup I have, but now that I've added a bunch of stuff to it, it can't detect it.
Engineer #2
Posted 16 June 2013 - 09:19 AM
Im going to revamp this code for you:


if fs.exists("derp") then
   local rf = fs.open("derp", "r")
   local content = {}
   for line in rf.readLine do
      if line:find( "shell%.run%(\"test\"%)" ) then -- Use strings, escape the the " with \ and 'magic' chars with %
           print("Found it!")
      end
   end
end

Here is an awesome site that shows you the magic characters: http://www.gammon.com.au/scripts/doc.php?lua=string.find
Enjoy :)/>
Edited on 16 June 2013 - 07:20 AM
Goof #3
Posted 16 June 2013 - 10:40 AM
Im going to revamp this code for you:


if fs.exists("derp") then
   local rf = fs.open("derp", "r")
   local content = {}
   for line in rf.readLine do
	  if line:find( "shell%.run%(\"test\"%)" ) then -- Use strings, escape the the " with \ and 'magic' chars with %
		   print("Found it!")
	  end
   end
end

Here is an awesome site that shows you the magic characters: http://www.gammon.co...lua=string.find
Enjoy :)/>
ehhm remember to close the fs.open :D/>


if fs.exists("derp") then
   local rf = fs.open("derp", "r")
   local content = {}
   for line in rf.readLine() do -- and remember parantheses :D/>
      if line:find( "shell%.run%(\"test\"%)" ) then -- Use strings, escape the the " with \ and 'magic' chars with %
           print("Found it!")
      end
   end
   rf.close()
end
BlueZero #4
Posted 16 June 2013 - 06:52 PM
Lol, thanks guys. This'll be of a lot more use than what I had, definitely.

-snip-

I remember finding that site before but that's when I was zombified. Lol, now that I see it, it makes sense where I screwed up. Thanks man. :D/>
MysticT #5
Posted 16 June 2013 - 07:12 PM
Another option to escaping magic characters would be to perform a plain search:

string.find(line, 'shell.run("herpherp-1.0/herpherp")', 1, true)
Also note that you can use ' for strings, so you don't need to escape the " too.
BlueZero #6
Posted 17 June 2013 - 07:20 AM
Another option to escaping magic characters would be to perform a plain search:

string.find(line, 'shell.run("herpherp-1.0/herpherp")', 1, true)
Also note that you can use ' for strings, so you don't need to escape the " too.

I swear I need to beat my head against this lua book I downloaded… Maybe I'll remember something then, lol. Thanks for that MysticT, that seemed to do the trick perfectly.
theoriginalbit #7
Posted 17 June 2013 - 07:21 AM
I swear I need to beat my head against this lua book I downloaded…
Out of curiosity. What book did you download?
BlueZero #8
Posted 17 June 2013 - 07:38 AM
-snip-

Something that'll probably make you facepalm but I downloaded Programming in Lua 2nd Edition.
theoriginalbit #9
Posted 17 June 2013 - 07:43 AM
Nah the PIL is probably the best one you could have got. The best version to get being the one for the version of Lua that ComputerCraft uses.
BlueZero #10
Posted 17 June 2013 - 07:49 AM
I don't know, I just don't find it to be soaking in properly. I read pages after pages and it's like, "WTf did I just read?". I think I might learn better from gradual complexity is code. I seem to understand by looking at code snippets and programming in it than really reading about it.

I wonder if that'd be a good book idea. Starting with very basic functions and declarations of it in example programs as they gradually get more and more complex and using different functions of lua.
theoriginalbit #11
Posted 17 June 2013 - 07:54 AM
Could be a good idea.

A lot of people find the PIL easier to read in this format, its less info at once then.
BlueZero #12
Posted 17 June 2013 - 08:30 AM
Another one of my problems I guess I've got is that I haven't taken any advanced math so some language they use in that book when referring to different functions I don't understand. Factorials and such like in that link you've got, I've never really understand that when I was reading through so I mostly just smiled and nodded, then tried to continue.
theoriginalbit #13
Posted 17 June 2013 - 08:40 AM
A factorial. represented by n!, where the result is the product of all the positive integers below n.

So 5! is
5 x 4 x 3 x 2 x 1 = 120

10! is
10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800

So that first example that they give you, takes an input from the user, converts it to a number, and prints the result of the factorial of that number.

Believe me, I didn't do any "advanced mathematics" either, but that is what Google is for ;)/>
BlueZero #14
Posted 17 June 2013 - 05:35 PM
Yeah. Lol, I donno. The only thing I found interesting enough when it came to learning to program was Code Academy. That was cool seeing as it was purely web user-interface driven. It teaches you python and php and such. They should add more languages. I learned python a little bit from there but in the end got disinterested seeing as I never used it for anything. Atleast with lua I've got an outlet to work with being computercraft. :P/>