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
word:=readln('hello, what is your word?');
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
your computer, they would only see a load of scribble where your user names and passwords were, and
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!
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
word:=readln('hello, what is your word?');
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
your computer, they would only see a load of scribble where your user names and passwords were, and
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!