strfind in text file :/
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

strfind in text file :/

Started by pHaTTy, 05 January, 2004, 22:27:02

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pHaTTy

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?
Resistance is futile!

NotRabidWombat

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


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

pHaTTy

Thanx a bunch dude, this will help me alot, cant thank you enough :))

Hmmmz but now im confused, that looks very confusing lol :/
Resistance is futile!

pHaTTy

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
Resistance is futile!

pHaTTy

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?
Resistance is futile!

c h i l l a

#5
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

pHaTTy

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 :)
Resistance is futile!

SMF spam blocked by CleanTalk