Jump to content




string.find


  • You cannot reply to this topic
13 replies to this topic

#1 BlueZero

  • Members
  • 41 posts

Posted 16 June 2013 - 08: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.

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 16 June 2013 - 08: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.co...lua=string.find
Enjoy :)

Edited by Engineer, 16 June 2013 - 08:20 AM.


#3 Goof

  • Members
  • 751 posts

Posted 16 June 2013 - 09:40 AM

 Engineer, on 16 June 2013 - 08:19 AM, said:

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


#4 BlueZero

  • Members
  • 41 posts

Posted 16 June 2013 - 05:52 PM

Lol, thanks guys. This'll be of a lot more use than what I had, definitely.

 Engineer, on 16 June 2013 - 08:19 AM, said:

-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

#5 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 16 June 2013 - 06: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.

#6 BlueZero

  • Members
  • 41 posts

Posted 17 June 2013 - 06:20 AM

 MysticT, on 16 June 2013 - 06:12 PM, said:

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.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 June 2013 - 06:21 AM

 BlueZero, on 17 June 2013 - 06:20 AM, said:

I swear I need to beat my head against this lua book I downloaded...
Out of curiosity. What book did you download?

#8 BlueZero

  • Members
  • 41 posts

Posted 17 June 2013 - 06:38 AM

 theoriginalbit, on 17 June 2013 - 06:21 AM, said:

-snip-

Something that'll probably make you facepalm but I downloaded Programming in Lua 2nd Edition.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 June 2013 - 06: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.

#10 BlueZero

  • Members
  • 41 posts

Posted 17 June 2013 - 06: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.

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 June 2013 - 06: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.

#12 BlueZero

  • Members
  • 41 posts

Posted 17 June 2013 - 07: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.

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 June 2013 - 07: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 ;)

#14 BlueZero

  • Members
  • 41 posts

Posted 17 June 2013 - 04: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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users