PtokaX forum

Development Section => Your Developing Problems => Topic started by: Jaras on 08 May, 2004, 15:51:50

Title: [Request]How to: do various things with files.
Post by: Jaras on 08 May, 2004, 15:51:50
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.
Title:
Post by: [NL]Pur on 08 May, 2004, 16:28:35
can you post the script that way we can help
Title:
Post by: Jaras on 08 May, 2004, 22:43:36
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'.
Title:
Post by: plop on 08 May, 2004, 23:39:10
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
Title:
Post by: Jaras on 09 May, 2004, 18:00:15
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
Title:
Post by: plop on 09 May, 2004, 19:43:14
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
Title:
Post by: NotRabidWombat on 09 May, 2004, 19:59:27
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
Title:
Post by: Jaras on 09 May, 2004, 23:26:25
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???
Title:
Post by: Jaras on 11 May, 2004, 01:01:58
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.