[Request]How to: do various things with files.
 

[Request]How to: do various things with files.

Started by Jaras, 08 May, 2004, 15:51:50

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Jaras

if there's someone, who could spend some time for writing such guide i'd be wery grateful. generally file operations are easy, but i found major problems with format of read data. when i read a line and then put in strfind =, ptoka says, that #1 argument is a table, not expected string, but before strfind i'm writing type() of that variable and type returns that's a string. i don't have any idea, what could be wrong so i'm asking for help.

[NL]Pur

can you post the script that way we can help

Jaras

#2
function GoodIP( user )
	readfrom( IPsFile )
	while 1 do
		local line = read()
		if not( line == nil ) or ( line == "" ) then
			user:SendData( BotName, line.." - "..type( line ) )
			if strfind( line, "(.+)%s=%s" ) == user.sNick then
				user:SendData( BotName, line )
				if strfind( line, "%s=%s(.+)$" ) == user.sIP then
					readfrom()
					return 1
				else
					readfrom()
					return nil
				end
			end
		else
			readfrom()
			return 0
		end
	end
end

IPsFile:
Jaras = 82.66.66.66
emu = 82.77.77.77

it's based on cSlave, coz i'm still learning. SendData are there for debuggin'.

plop

try like this.
function GoodIP( user )
	readfrom( IPsFile )
	while 1 do
		local line = read()
		if not( line == nil ) or ( line == "" ) then
			user:SendData( BotName, line.." - "..type( line ) )
         local s,e,tmp = strfind( line, "(.+)%s=%s" )
			if tmp == user.sNick then
				user:SendData( BotName, line )
            local s,e, tmp2 = strfind( line, "%s=%s(.+)$" )
				if tmp2 == user.sIP then
					readfrom()
					return 1
				else
					readfrom()
					return nil
				end
			end
		else
			readfrom()
			return 0
		end
	end
end
the strfind you posted above returns 1 if the pattern matches something.
this means it never can be equal 2 and ip or name.

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Jaras

#4
i tried giving to a variable strfind value with the same result. i'll try your way, but i don't want to copy i want to understand. i thought that i understood cSlave's method, but it didn't work, that's why i asked for guide.

of course thank you kindly for help  :D

plop

QuoteOriginally posted by Jaras
i tried giving to a variable strfind value with the same result. i'll try your way, but i don't want to copy i want to understand. i thought that i understood cSlave's method, but it didn't work, that's why i asked for guide.

of course thank you kindly for help  :D
s,e,tmp = strfind( line, "(.+)%s=%s" )
=
returns_1_if_found,returns_the_location_if_found,returns_what_is_found = strfind(data, ..........etc

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

NotRabidWombat

Almost.

"strfind (s, pattern [, init [, plain]])
Looks for the first match of pattern in s. If it finds one, then strfind returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. If the pattern specifies captures (see gsub below), the captured strings are returned as extra results. A third, optional numerical argument init specifies where to start the search; its default value is 1, and may be negative. A value of 1 as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain ``find substring'' operation, with no characters in pattern being considered ``magic''. Note that if plain is given, then init must be given too."

http://www.lua.org/manual/4.0/manual.html

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Jaras

QuoteOriginally posted by plop
QuoteOriginally posted by Jaras
i tried giving to a variable strfind value with the same result. i'll try your way, but i don't want to copy i want to understand. i thought that i understood cSlave's method, but it didn't work, that's why i asked for guide.

of course thank you kindly for help  :D
s,e,tmp = strfind( line, "(.+)%s=%s" )
=
returns_1_if_found,returns_the_location_if_found,returns_what_is_found = strfind(data, ..........etc

plop

damn. how could i've forgotten???

Jaras

sry 4 doublepost, but i just realized how dumb i am. you made me believe, that i've got problems with using strfind function (and it was stupid mistakes), but i was asking bout strfind itself, not how to use it. short flashback: i'm reading a line from file and putting it into strfind as firts argument. before that i'm writing type() of variable holding this line. it returns, that variable's type is string, but ptoka says, that #1 argument in strfind got table, when string was expected. so this is main problem. can anyone help? again - i've started new thread, coz i want to understand it, not learn it.

SMF spam blocked by CleanTalk