PtokaX forum

Development Section => Your Developing Problems => Topic started by: pHaTTy on 05 January, 2004, 22:27:02

Title: strfind in text file :/
Post by: pHaTTy on 05 January, 2004, 22:27:02
hmm hi fellow scripters/forum members

i have a problem with something i have low experience with the way i am doing this, i hope it can be done this way please help :/

function ReadScores()
readfrom("Scores.dat")
while 1 do
line = read()
s,e,name,point = strfind(line,"%b<>%s+(%S+)%s+(%S+)")
if line == nil then
break
end
ScoreTotal[name] = point
readfrom()
end
end

is it possible for something like this to work?
Title:
Post by: NotRabidWombat on 05 January, 2004, 23:06:43
What you are trying to do is called serialization (storing a memory object to persitant data). There is a pickle function that does this and I wrote my own simple version of serialization for JumbleFever.
function Serialize(tTable, sTableName, hFile, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(hFile, "hFile equals nil");

assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");

sTab = sTab or "";

write(hFile, sTab..sTableName.." = {\n" );

for key, value in tTable do
local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);

if(type(value) == "table") then
Serialize(value, sKey, hFile, sTab.."\t");
else
local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
write(hFile, sTab.."\t"..sKey.." = "..sValue);
end

write(hFile, ",\n");
end

write(hFile, sTab.."}");
end
If you use serialize to create your score file from the score table, all you have to do is call dofile("filename.txt") to reload the scores.

-NotRabidWombat
Title:
Post by: pHaTTy on 05 January, 2004, 23:10:06
Thanx a bunch dude, this will help me alot, cant thank you enough :))

Hmmmz but now im confused, that looks very confusing lol :/
Title:
Post by: pHaTTy on 05 January, 2004, 23:48:30
ooops i just realised how stupid my mistake was lol


function ReadScores()
readfrom("BaNDiT/Scores.dat")
while 1 do
line = read()
if line == nil then
break
end
local s,e,name,point = strfind(line,"(%S+)%s+(%S+)")
ScoreTotal[name] = point
readfrom()
end
end


l8rr
Title:
Post by: pHaTTy on 06 January, 2004, 00:26:35
ok thought it was but not, its only reading the first line, and putting into memory, is there a way to read each line 1 by 1 until it comes to nil?
Title:
Post by: c h i l l a on 06 January, 2004, 00:46:01
function ReadScores()
readfrom("BaNDiT/Scores.dat")
while 1 do
line = read()
if line == nil then
break
end
local s,e,name,point = strfind(line,"(%S+)%s+(%S+)")
ScoreTotal[name] = point
readfrom()
end
end

that code only reads one line *edit* hmm no it should work but looks weird**,  but truly once you have a table I would serialise it, then you just do dofile + you don't need to re strfind reread it again ;)

but maybe this will help anyways

function ReadScores()
local handle = openfile("BaNDiT/Scores.dat","r")
    line = read(handle)
while line do
local s,e,name,point = strfind(line,"(%S+)%s+(%S+)")
ScoreTotal[name] = point
      line = read(handle)

end
end
Title:
Post by: pHaTTy on 06 January, 2004, 00:57:35
i will keep that in mind, i just trying to learn the bits im not good at, so whilst i script i might as well learn them :))

thx :)