Official Soldat Forums
Server Talk => Scripting Discussions and Help => Topic started by: VinceBros on August 03, 2010, 04:26:21 pm
-
Hello,
I am recently practicing my scripting and there's something i've always wondered..
How do you script something that can search a .txt/.log/.ini and returns how many lines it has?
And also, i bet this is using in "sorting", if possible i'd like to have an exemple of how to sort..
Thanks :)
-
Use ReadFile, look for newline/carriage returns. 0x0A and 0x0D iirc.
-
http://soldatcentral.com/index.php?page=script&f=129 <- spliting into something (for example Explode(ReadFile(MyFile.txt), #13#10) will split MyFile into array of lines)
http://forums.soldat.pl/index.php?topic=38524.0 <- sorting
-
Actually i needed an explanation, not a link to those files i already know ::)
Thanks anyways.
-
Here, i had this laying around.
function ContainsNum(Source,Test: string): integer;
var temp: integer;
begin
result := 0;
repeat
temp := pos(test,source);
if temp = 0 then exit;
result := result + 1;
delete(source,1,temp);
until temp = 0;
end;
You would use it like this:
lines := containsnum(readfile(MYFILE),#13+#10);
-
hey, i've been messing up with these a bit and i'm wondering something..
var
i: integer;
j: array of string;
function explode(const Delimiter: string; Source: string): array of string;
var
Position, DelLength, ResLength: integer;
begin
DelLength := Length(Delimiter);
Source := Source + Delimiter;
repeat
Position := Pos(Delimiter, Source);
SetArrayLength(Result, ResLength + 1);
Result[ResLength] := Copy(Source, 1, Position - 1);
ResLength := ResLength + 1;
Delete(Source, 1, Position + DelLength - 1);
until (Position = 0);
SetArrayLength(Result, ResLength - 1);
end;
function OnPlayerCommand(ID: integer; Text: string): boolean;
begin
if text = '/test' then begin
j := explode(chr(13)+chr(10),ReadFile('test.txt'));
for i := 0 to GetArrayLength(j)-2 do begin
WriteConsole(0,j[i],$ffffff);
end;
end;
end;
On that piece of code, everything works, i'm just wondering why do i have to do -2
after GetArrayLength(J)
and not - 1 ?
It seems like when it gets array length it always get 2 more values in the array of string, why that?
Also when i changed j: array of string;
to j: array [1..100] of string;
I was getting Type Mismatch in OnPlayerCommand, why?
Thank you again buddies :3
-
You cannot assign an array that you define with [x..y] using :=, you also cannot call setarraylength or getarrraylength one an array defined in such a way.
Try writeln()'ing out an explode() of a file of which you know the contents and see what it is putting at the end.
-
probably #13#10 in the end, like always when you use ReadFile -.-
-
@HackTank: I'll try that, thank you.
But what does SetArrayLength does? Set a length to an array?
@dnmr: No that's because this explode version is like upside down, look at it :p
EDITED.
-
you can define array's length when you define array itself, like
var mystaticarray: array [0..5] of variant;
or you can define it as dynamic array, like
var mydynamicarray: array of variant;
then, it has no length, what means that even calling mydynamicarray[0] := 1234 will fail with error 'Out of Range', since this array has no length at all.
When you do SetArrayLength(mydynamicarray, 5) it will set your array length to 5, what means that mydynamicarray[0-4] are available for you.
-
OK thanks i get it now.
In HackTank's ContainsNum/Split/Explode functions, what does Delete(x,x,x) actually do?
-
OK thanks i get it now.
In HackTank's ContainsNum/Split/Explode functions, what does Delete(x,x,x) actually do?
Syntax: Delete(mystr, nStart, nHowmany)
where mystr is a string identifier; and
nStart and nHowmany are integers
Delete eliminates nHowmany characters from mystr starting with the nStartth character.
After the following code fragment is executed, mystr will be 'cool'.
mystr := 'school';
Delete(mystr, 3, 1);
Delete(mystr, 1, 1)
-
Kewl thanks,
I've got another question, this ain't gonna end yet haha.
Well yeah, may i ask what does Pos(x1,x2) does?
I've tried it with #13#10 as x1 and x2 as a SourceFile, but i actually don't get what it returns..
-
http://www.lmgtfy.com/?q=pascal+pos
-
I thought it had a difference with StrPos.
And gawsh, i gotta use google more.