SRL Archive - General

Scar Method issue: GetArrayLength


tylanbo

2007-10-17 23:48:09

I was making a database of bitmaps for use with my scripts, and decided to change some redundant code to something more efficient/complicated.

I had done this many times in java, the language I am tought in school, but it doesn't work in pascal. The syntax should be correct…

Anyways, here is what it looks like in my bitmap tester:

procedure ListFound;
var
i:Integer;
begin
writeln('Bitmaps that were not found:');
for i:= 0 to GetArrayLength(NotFound) do
begin
if length(NotFound[i]) > 0 then
writeln(NotFound[i]);
end;
writeln('Bitmaps that were found:');
for i:= 0 to GetArrayLength(Found) do
begin
if length(Found[i]) > 0 then
writeln(Found[i]);
end;
end;

Every time it found a Bitmap, I had it store the string of which bitmap it was into the array, and that way when it lists what were found/not found so I know immediately which one was buggy. Simple enough. If no value was put into the array, it is fair to assume it will hold an array length of 0. Here is how it was created:

Found : Array [0..100] of String;
NotFound : Array [0..100] of String;

I can get it to work using a separate variable to count array length, but I shouldn't have to. Please make sure that this method is working correctly.

GetArrayLength didn't work when not given a value, and i'm sick of dealing with it.

Bobarkinator

2007-10-18 01:51:39

I don't know what your problem is but his works:
program New;
{.include SRL/SRL.scar}
var
Found : TStringArray;
NotFound : TStringArray;

procedure ListFound;
var
i:Integer;
begin
writeln('Bitmaps that were not found:');
for i:= 0 to GetArrayLength(NotFound)-1 do
begin
if length(NotFound[i]) > 0 then
writeln(NotFound[i]);
end;
writeln('Bitmaps that were found:');
for i:= 0 to GetArrayLength(Found)-1 do
begin
if length(Found[i]) > 0 then
writeln(Found[i]);
end;
end;

begin
SetArrayLength(Found,5);
SetArrayLength(NotFound,5);
Found[0]:='lkjsf';
Found[1]:='lkjdf';
Found[2]:='lkjds';
Found[3]:='dlkjdls';
Found[4]:='lkjsa';
NotFound[0]:='lksjdf';
NotFound[1]:='slkjda';
NotFound[2]:='slkja';
NotFound[3]:='lsaf';
NotFound[4]:='slkjal';
ListFound;
end.

GoF

2007-10-18 06:15:17

0 Counts as a number too.. Meaning that if your array is for example [0..10] and you use


for Something := 0 to GetArrayLength(ArrayName) do

It will go from 0 to arraylength (11) and you will get a Out of Range error, so you need to add a - 1 there…


for Something := 0 to GetArrayLength(ArrayName) - 1 do


And that will work.. Unless the problem wasnt about this.

tylanbo

2007-10-18 20:55:22

I suppose my question is, when the array is initialized is it given values to start? Does it become all 0's or do they fill the slots with 'null'.

null cannot be greater than 0, so that would throw an exception, but if it is '0' then it should work.

R0b0t1

2007-10-20 13:50:12

"null" means non-existent (Its actually "nil" in Delphi). Therefore, you are referencing something that does not exist. I'm pretty sure thats whats wrong…?


For the Idiots: Null/Nil means that the value does not have a memory space assigned to it. Like, for a value of 0, and integer has a memory space that says "0", but for a null value I believe there is no memory assigned to it. (EX: Having a value of zero is like a strings "space" character (' '), while null is like an empty string ('').

I'm also fairly sure that an array of strings is an "Array of Array of Char", with each string being an array…


Var
Strings: Array of Array of Char;

begin //This is like an array of two strings, 'Hello' and 'World'
Strings[0][0]:= 'H';//This creates a string 'Hello'
Strings[0][1]:= 'e';//This is the same as saying:
Strings[0][2]:= 'l';// StringArr[0]:= 'Hello'; , as you can reference strings
Strings[0][3]:= 'l';// characters like an array.
Strings[0][4]:= 'o';
Strings[1][0]:= 'W';//Same as saying:
Strings[1][1]:= 'o';// StringArr[1]:= 'World';
Strings[1][2]:= 'r';
Strings[1][3]:= 'l';
Strings[1][4]:= 'd';
end.

Like, if you had a string called 'Str1' that held the value 'NooB', the variable "Str1[1]" would hold the value of 'N' and so forth.

tylanbo

2007-10-21 19:14:49

From: R0b0t1
"null" means non-existent (Its actually "nil" in Delphi). Therefore, you are referencing something that does not exist. I'm pretty sure thats whats wrong…?


For the Idiots: Null/Nil means that the value does not have a memory space assigned to it. Like, for a value of 0, and integer has a memory space that says "0", but for a null value I believe there is no memory assigned to it. (EX: Having a value of zero is like a strings "space" character (' '), while null is like an empty string ('').

I'm also fairly sure that an array of strings is an "Array of Array of Char", with each string being an array…


Var
Strings: Array of Array of Char;

begin //This is like an array of two strings, 'Hello' and 'World'
Strings[0][0]:= 'H';//This creates a string 'Hello'
Strings[0][1]:= 'e';//This is the same as saying:
Strings[0][2]:= 'l';// StringArr[0]:= 'Hello'; , as you can reference strings
Strings[0][3]:= 'l';// characters like an array.
Strings[0][4]:= 'o';
Strings[1][0]:= 'W';//Same as saying:
Strings[1][1]:= 'o';// StringArr[1]:= 'World';
Strings[1][2]:= 'r';
Strings[1][3]:= 'l';
Strings[1][4]:= 'd';
end.

Like, if you had a string called 'Str1' that held the value 'NooB', the variable "Str1[1]" would hold the value of 'N' and so forth.


I'm aware what null means.

I'm not sure whether this is true or not, but i believe int arrays are initialized with 0, chars with ' ' ( which is a value of 32 i believe ) , and strings with 'null'.

At least that's the way it is in java. This would answer my question, but I didn't check the validity.

R0b0t1

2007-10-23 22:59:02

Hmmm… I think everything is right except strings, which I believe are '' (same thing I think), and chars, I'm pretty sure its ('') too.

Meh, don't ask me. I'm full of crap.

Pyro

2007-10-24 04:36:19

Just so you know. Im not sure about it. I think actually stored with 0. As null only just got put into scar? But also your for loop will have to be

for i:=0 to getarraylength(notfound)-1 do

notice the minus one. As say you have 15 values in the array. The array length will return 15. But the numbers (starting from 0) will only go up to 14.

tylanbo

2007-10-29 18:18:19

From: Pyro
Just so you know. Im not sure about it. I think actually stored with 0. As null only just got put into scar? But also your for loop will have to be

for i:=0 to getarraylength(notfound)-1 do

notice the minus one. As say you have 15 values in the array. The array length will return 15. But the numbers (starting from 0) will only go up to 14.


Yes, that would be true, but I actually start the for loop at 1 instead of 0.

I believe I am going to slap myself, if I am correct, that GetArrayLength was intended for variable arrays, not " blah := Array [0..1] of Integer; "
If that is the case then I wasted your time, sorry =|

Speaking of variable arrays, or array lists, is there a tut here for one? I think I saw a brief section, but could never find it again.

Pyro

2007-10-31 01:10:06

From: tylanbo
Yes, that would be true, but I actually start the for loop at 1 instead of 0.

I believe I am going to slap myself, if I am correct, that GetArrayLength was intended for variable arrays, not " blah := Array [0..1] of Integer; "
If that is the case then I wasted your time, sorry =|

Speaking of variable arrays, or array lists, is there a tut here for one? I think I saw a brief section, but could never find it again.


Yes quite right you are. Getarraylength only works if the array has been set using setarraylength. not set using array [0..4] etc

tylanbo

2007-10-31 01:59:52

From: Pyro
Yes quite right you are. Getarraylength only works if the array has been set using setarraylength. not set using array [0..4] etc


I was considering GetArrayLength to have the same functionality as .length in java. Obviously, I was wrong.

Thank you all for helping me with this, I know it would have been a lot easier if I had shown myself defining the array. Issue has been resolved.