PDA

View Full Version : Using an array as a function result

Distort
08-25-2007, 09:46 AM
Using a function as a array
By Distort

Credits

n3ss3s/bullzeye95 - Showing me some redundancy in the final example I suppose.

SCAR Versions

Works!
SCAR 3.11 Divi Tested
Doesn't work.
Yet to test all others

Introduction
Hello to whomever may read this, this text is about how to use a function as a array. I cannot
fathom a reason to use a function as a array, but if you find a idea about it, please let me know.
I wrote this because it was something I thought of using in a encrypter/decrypter I am using in
one of my upcoming scripts (on first run, it will encrypt user names/passwords in a .ini, and
decrypt again when called upon) and found it quite hard to implement a function as a array. Then,
I found out how to work it and I am sharing this knowledge with you all because it's easier to
read about it than to spend... some time figuring out how to work it.

So in short, if you find out how to use it in any example other than my example, drop me a line.

Declaring

To declare a function as a array, we go about how we usually declare functions and how we usually
declare arrays. To do this, you can do something like this:

function hello(text:string):array of string;

So what I'm doing is declaring the function hello, which is a array of a string, and I am asking
it to allow a string as a parameter.

Scripting it

Now we have declared it, we can just program it to do what we want. In my case, I'm going to
first set the array to the length of the string and then go through each character in the string
and dedicate one part of the array per character (as I said, it was a learning curve for a
encrypter/decrypter).

function hello(text:string):array of string;
var
a, b : integer;

begin
a:=length(text);
{Get the length of the string "text", and store it as integer "a"}
setarraylength(result, a);
{Set the array length to that of the integer "a".
For example, if we had "text" as funny, "a" would be 5, and
that would mean the function would be hello[0..4], or 5 arrays.
we could have set this when declaring the function but
I want to use a different word each time and not use a lot of
memory kept to story [0..999] arrays =) .}
for b:=1 to a do begin //A simple for loop
result[b-1]:=copy(str,b,1);
{Ok, what this will do is store the letter at whatever
position is in the loop, and will store it in the array at
the position -1. For example, if we used the word "funny", it would store
"f" at the array [0], and "u" at the array [1], etc.}
end;//End the for loop
end;//End the function.

Using it
Now comes the really annoying part (well, when trying to work it out anyway). We now have a array
called "hello", and it can have as many characters we want stored in it but how can we use it?

This is the part I found hard, because you cannot use it like a regular function. If we were to
try "function hi():string" we could do "writeln(hi());", but we cannot do "writeln(hello('bob'));"
because we have not said what position we want it at.
Then we try to declare the position, EG hello('bob')[0];, hello[0]('bob');, etc etc etc. but it
all doesn't work.

Then, in the main loop, I decided to leave it all on it's own, like so:

begin
hello('bob')
end.

And it compiled. This left me thinking "well, now I have a array, but I cannot use it because it is destroyed when it's finished the function". Then, it hit me, what if I declared a array.

program arrayasafunction;
var
a:array of string;
word : string;

function test(str:string):array of string;
var
a,b:integer;
begin
a:=length(str);
setarraylength(result,a);
for b:=1 to a do begin
result[b-1]:=copy(str,b,1);
end;
end;

begin
a:=test(word);
writeln(a[0]);
end.

Eureka!

Conclusion
Now that you know how to use it, think of reasons why to use it. For me, it's to store a .ini
file which would include encrypted user names and passwords, so if some scoundrel would get into
would need to get your own key to decrypt it all.

Remember, your not just limited to a array of strings ;)

End notes
If you think I'm wrong anywhere, and can show me and correct me where, please feel free to post or
PM me and I will be happy to adjust my notes about how to implement a functional array and with
your name stated as a helper. Also, if you wish to write about how cool this text is, I dare say
you are mental and in need of a right good kicking, but a compliment does go a long way. If you
don't like this, please state why and I will try to adjust the text.

For now, so long suckers!

n3ss3s
08-25-2007, 10:10 AM
The title is pretty misleading, correct would be "Using an array as a result".

The function returns the array there.

Using a function as an array would be like:

function CreateArrayIndex(Stuff: Variant; TheArray: TVariantArray): TVariantArray;
begin
SetArrayLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := Stuff;
end;

and then a function that pics something from the array.

Anyways, good job I guess.

Distort
08-25-2007, 10:14 AM
o.O, I didn't think that the title was misleading, however if a mod or admin (which I think a KBD is?) can change the title to something more... appropriate I would be thankful.

Markus
08-25-2007, 11:40 AM
the ranks (kbd, lesser demon, chicken) wont make you a mod.

bullzeye95
08-25-2007, 12:22 PM
Just to let you know, you don't have to set the length of an array before setting it's value to another array. It automatically does it ;)

Distort
08-25-2007, 12:32 PM
I'd rather set it myself just to make sure it isn't using a load of memory and isn't redundant.

n3ss3s
08-25-2007, 02:16 PM
It does? but then you can tell me why does this give a runtime error!
Haha!

program New;
var
A: array of Integer;
begin
A[0] := 10;
end.

pwnt.

o.O, I didn't think that the title was misleading, however if a mod or admin (which I think a KBD is?) can change the title to something more... appropriate I would be thankful.

But if I would be an undercover admin? :p Jk/Jk Dont ban me, Jk/Jk

Distort
08-25-2007, 02:26 PM
That's not setting it to the length of another array, and you havn't set it's length at all.

If you were to do this:
var
A: array [0..1] of Integer;

or

var
A: array of Integer;
begin
setarraylength(A,1);

You would not get a run time error. Because it doesn't do it automatically.

And you would not get a run time error doing this either:

var
A: array of integer;
B: array [0..99] of integer;
begin
A:=B;

because it replicates B.

Although that isn't what you were taking about in your first post, in your first post you were arguing about these 2 lines:

setarraylength(a,length(word));
a:=test(word);

and I now know why you were arguing about it :)

But your second post doesn't back it up unless you can argue against it. For now, your in the contributers list and let that be a lesson to you!

n3ss3s
08-25-2007, 02:29 PM
-.- IT DOES get the runtime error because I didnt declare the length, gives outta range, AND I was proving that to bullzeye, since he said that it does it automatically.

But your second post doesn't back it up unless you can argue against it. For now, your in the contributers list and let that be a lesson to you!

Say that to Bullzeye :D

Distort
08-25-2007, 02:32 PM
Ye... I hardly ever read the names on forums, i just saw you both had part of the same signature (dam you that how to keep a idiot busy, it's been 3 weeks, come tell me how!) and i thought you were the same person :X

n3ss3s
08-25-2007, 02:33 PM
Haha lol, take that Bullzeye! :D

Distort
08-25-2007, 02:49 PM
Remember, I confused you for him, so take that n3ss3s! :P

n3ss3s
08-25-2007, 02:55 PM
=D

Starblaster100
08-25-2007, 07:20 PM
A very nice tutorial

Here is a practical use for using Arrays in Results:

program New;

var
MyArray, MyArray2: Array of Variant;
i: Integer;

{-------------------------------------------------------------------------------
| procedure Debug(What: Variant); |
| So I can simply Writeln any Array Type without worying about IntToStr etc. |
-------------------------------------------------------------------------------}

procedure Debug(What: Variant);
begin
Writeln(What);
end;

{-------------------------------------------------------------------------------
| function DelInArray(IndexNo: Integer; InArray: TVariantArray): TVariantArray |
| Deletes the Index No. from the Input Array and Outputs the Result |
-------------------------------------------------------------------------------}

function DelInArray(IndexNo: Integer; InArray: TVariantArray): TVariantArray;
var
i: Integer;
begin
SetArrayLength(Result, GetArrayLength(InArray)-1);
If (GetArrayLength(InArray) <> IndexNo) then
begin
For i := 0 to IndexNo-1 do
Result[i] := InArray[i];
For i := IndexNo to GetArrayLength(InArray)-1 do
Result[i-1] := InArray[i];
end else
For i := 0 to GetArrayLength(Result)-1 do
Result[i] := InArray[i];
end;

{-------------------------------------------------------------------------------
| Main Loop |
-------------------------------------------------------------------------------}

begin
ClearDebug;
MyArray := ['a','b','c','d','e','f','g']; //Setup a Test Array
//MyArray := [1, 2, 3, 4, 5, 6, 7, 8];

MyArray2 := DelInArray(7, MyArray); //Delete the specified index Number

For i := 0 to GetArrayLength(MyArray2)-1 do //Print the Result
Debug(MyArray2[i]);
end.

Lalaji
08-26-2007, 12:22 AM
I still dont get it completely, but nice

bullzeye95
08-26-2007, 12:49 AM
n3ss3s, I meant that you don't have to set the length of an array before you make it equal another one (IE, myArr1:= myArr2).

Distort
08-26-2007, 11:14 AM
n3ss3s, I meant that you don't have to set the length of an array before you make it equal another one (IE, myArr1:= myArr2).

Thus why you are in the credits list for showing me a redundancy.

lalaji, what exactly is it that you do not get and I'll try to explain it.

And starblaster... nice, I didn't think that you could have deleted records from within the array like that. How would that be useful in a RS script? Possibly to delete something within a array, but what o.O

n3ss3s
08-26-2007, 11:18 AM
n3ss3s, I meant that you don't have to set the length of an array before you make it equal another one (IE, myArr1:= myArr2).

Im getting confused, plz dont explain anymore to me :D

Starblaster100
08-26-2007, 01:27 PM
And starblaster... nice, I didn't think that you could have deleted records from within the array like that. How would that be useful in a RS script? Possibly to delete something within a array, but what o.O

It could be used for anything.

Finding a bunch of colors using FindColorsSpiralTolerance, and deleting the coords you don't want from that array

Searching a load of predefined coords and deleting each one as you do so, so you don't search there again

etc. etc.

n3ss3s
08-26-2007, 01:49 PM
Thanks Star you gave me idea to go and make FindColorsSkipBoxSpiralTolerance =)

It will propably just find as FindColorsSpiralTolerance, but then parse off the ones that are in da skip box... Should I make one supporting SkipBoxArray too?

Distort
08-26-2007, 06:35 PM
"FindColorsSkipBoxSpiralTolerance" .... I'd HATE to type that a lot lol.

n3ss3s
08-26-2007, 07:00 PM