SRL Archive - OSR Advanced Scripting Tutorials

TStringList Tutorial (Explanation + Example)


Freddy1990

2006-11-22 17:30:02

In this tutorial we're going to discuss the TStringList component.
This component is really recommended for people who want to save to and load from files but don't want to use scar's standard file functions.
Well, it's only partially a tutorial, instead of explaining the steps towards building an example i'm going to explain the component in general.
You could kind of call this component an enhanced array of string…
It contains an array of string where we stoire our lines, but it has alot more stuff than just that, so i'm going to explain some of those properties:

- Append
Adds a line (string) to the component. (separated by a return from the other lines)

- Text
All of the lines in a row separated by returns.

- Clear
Emptys the lines.

- Count
Returns the number of lines in the component.

- Strings
The array of string that contains the lines. (Strings[index])

- LoadFromFile
Loads lines from a file into the array of string.

- SaveToFile
Saves the lines to a file.

- Free
Frees the component from the memory. (Always do this when you don't need it anymore to prevent memory leaks)


Of course there are alot more of propertys, but these are the most useful ones.

Now here's an example to use all of this:
program TStringListExample;

var // Variable declarations
Strings: TStringList;
Count, a: Integer;

begin
// Initialises the TStringList component.
Strings := TStringList.Create;

// Inputs the lines into the component.
Strings.Append('Testingline 1');
Strings.Append('Testingline 2');
Strings.Append('Testingline 3');

// Saves the lines to ..\SCAR\TStringListExample.txt
Strings.SaveToFile(AppPath + 'TStringListExample.txt');

// Clears the lines
Strings.Clear;

// Loads the lines from ..\SCAR\TStringListExample.txt
Strings.LoadFromFile(AppPath + 'TStringListExample.txt');

// Gets the number of lines in the component and stores them into the variable "Count".
Count := Strings.Count;

// Writes the text that is in the component aka all the lines together.
WriteLn('The TStringList contains the following text: ' + Strings.Text);

// Writes the ammount of lines that are in the component.
WriteLn('The list contains ' + IntToStr(Count) + ' lines:');

// Writes all of the lines 1 by 1.

for a := 1 to Count do
WriteLn(' ' + IntToStr(a) + ': ' + Strings.Strings[a - 1]);

// Frees the component.
Strings.Free;
end.

WhiteShadow

2006-11-22 23:55:55

I learned.

Thanks ALOT, Freddy. One of the best scripters I know of. (:

Freddy1990

2006-11-23 00:55:24

Heh, thankx, ur welcome :) ;)

h4ph4z4rd

2006-12-29 23:14:11

+rep

Great stuff! thanks. Love the format and simplicity it's explained by =)

I'm looking foward to a guide on Integer/Extended hopefully? REALLY hard to tell the difference about them and when to use them =S

Thanks =)

~faith

JAD

2007-02-23 11:05:13

i had to read it over 3 times though, cause i got lost the first 2 :P IDK if i was zoning out or i was just confused lol. good tut though.