Author Topic: Need some explanation about "lines"  (Read 2058 times)

0 Members and 1 Guest are viewing this topic.

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Need some explanation about "lines"
« 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 :)

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: Need some explanation about "lines"
« Reply #1 on: August 03, 2010, 08:23:49 pm »
Use ReadFile, look for newline/carriage returns. 0x0A and 0x0D iirc.

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: Need some explanation about "lines"
« Reply #2 on: August 04, 2010, 06:22:21 am »
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
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: Need some explanation about "lines"
« Reply #3 on: August 04, 2010, 08:14:05 am »
Actually i needed an explanation, not a link to those files i already know  ::)

Thanks anyways.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: Need some explanation about "lines"
« Reply #4 on: August 04, 2010, 11:01:29 am »
Here, i had this laying around.

Code: [Select]
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:
Code: [Select]
lines := containsnum(readfile(MYFILE),#13+#10);
« Last Edit: August 04, 2010, 11:03:15 am by Hacktank »


Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: Need some explanation about "lines"
« Reply #5 on: August 07, 2010, 11:22:36 am »
hey, i've been messing up with these a bit and i'm wondering something..
Code: (pascal) [Select]
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
Code: [Select]
-2 after
Code: [Select]
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
Code: [Select]
j: array of string; to
Code: [Select]
j: array [1..100] of string;I was getting Type Mismatch in OnPlayerCommand, why?


Thank you again buddies :3
« Last Edit: August 07, 2010, 11:26:15 am by VinceBros »

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: Need some explanation about "lines"
« Reply #6 on: August 07, 2010, 12:50:34 pm »
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.


Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Need some explanation about "lines"
« Reply #7 on: August 07, 2010, 12:54:48 pm »
probably #13#10 in the end, like always when you use ReadFile -.-

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: Need some explanation about "lines"
« Reply #8 on: August 07, 2010, 03:31:02 pm »
@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.

Offline Falcon`

  • Flagrunner
  • ****
  • Posts: 792
  • A wanted lagger
Re: Need some explanation about "lines"
« Reply #9 on: August 07, 2010, 03:35:59 pm »
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.
If you're not paying for something, you're not the customer; you're the product being sold.
- Andrew Lewis

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: Need some explanation about "lines"
« Reply #10 on: August 08, 2010, 08:55:54 pm »
OK thanks i get it now.

In HackTank's ContainsNum/Split/Explode functions, what does Delete(x,x,x) actually do?

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Need some explanation about "lines"
« Reply #11 on: August 08, 2010, 09:17:49 pm »
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)

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: Need some explanation about "lines"
« Reply #12 on: August 08, 2010, 09:57:42 pm »
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..

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Need some explanation about "lines"
« Reply #13 on: August 08, 2010, 10:20:03 pm »

Offline VinceBros

  • Soldat Beta Team
  • Camper
  • ******
  • Posts: 275
Re: Need some explanation about "lines"
« Reply #14 on: August 08, 2010, 10:51:26 pm »
I thought it had a difference with StrPos.

And gawsh, i gotta use google more.