Page 1 of 1

2 Errors with arrays

PostPosted: Sep 5, 2005 @ 6:29pm
by mmtbb
I tried this code but am getting an error. Is there a bug, or am I using it wrong:

Dim(tt$,12);
showmessage(count(tt$)); shows 0
for(i$, 0, 11)
dd$ = "1,2,3";
strtolist(dd$, ",", tt$[i$]);
showmessage(tt$[i$][0]);
end;

This will work great BUT only loop 3 times (the number of delimiters in dd$) then give me an error.

Technically shouldn't this give me variables
tt$[0][2] up to tt$[11][2]?

Also the count function says there are 0 elements in the array I just created. Shoudn't it return 12?

Finally, perhaps all this is behaving this way because I am using a list (though I want to use an array). If so, there needs to be a function that shows the upper bound of an array, and allows a StrToArray function also.

PostPosted: Sep 6, 2005 @ 1:54am
by bmanske
mmtbb,

If PPL variables were truely free form, then you might be able to do this, but...

The first Dim is creating an array of TDoubles (8 bytes each). It looks as though you are trying to take each TDouble and use it as a list variable. This doesn't work because there is other data attached to the list like a pointer into the list.

You go on to express the desire to turn the list into an array. You could use StrToList then use ListToArray to make that conversion.

The Count function counts the elements in a list and I don't know what PPL will do for the assignment TT$[I$].

To get the upper limit of an array use the SizeOf function.

You are able to create a multi-dimentional array with the Dim function. Maybe that would work for you. If you let us know a little more about your project, then maybe we can point you in the right direction.

bmanske

PostPosted: Sep 6, 2005 @ 4:32am
by mmtbb
Dim(tt$,12);
showmessage(count(tt$)); shows 0
for(i$, 0, 11)
dd$ = "1,2,3";
strtolist(dd$, ",", tt$[i$]);
showmessage(tt$[i$][0]);
end;

One thing that I didn't show is farther down the code. This code is important because I need to create an array that holds the split string of numbers read from a file. This is used to define the object that is created later:

#object myclass theclass$(tt$[i$][0],tt$[i$][1],tt$[i$][2]);

remember, this all works perfectly (i.e. a class is created with all the proper construction) UNTIL I get past the size of the elements the are created from dd$. This seems screwy.

Let me even make it more clear. I have a class with 3 public variables: Name$, Size$, and Year$. I have 25 objects that need to be created. They are stored in a text file. like this:

Gordon, 123, 1996
Jim, 202, 1980
...and many more

At the beginning of my program, I open the file, do a loop and read each line to a variable, split the variable into three and use those three to feed to the constructor of the class.

Ok. Right now if I created People$[24] with 25 elements. Then used those people to each hold the 3 arguments to put through the constructor. So, this should allow my to make:
People$[0][0]
People$[0][1]
People$[0][2]
------------
People$[1][0]
People$[1][1]
People$[1][2]
------------
People$[24][0] //all the way to 24
People$[24][1]
People$[24][2]

My code should work. I already defined the first dim to make 25 people. I never mess with that again. My code is supposed to add the three args to the second dimension. However, it is giving me errors at people$[3][0] which it will not let me create. If I increaed the args to 4, Name Size, Year, Hair, then it would let me make one more set up to people[4][0]. I hope this makes sense. My code shouldn't effect the first dimension of people, but it is.

PostPosted: Sep 6, 2005 @ 1:02pm
by bmanske

PostPosted: Sep 6, 2005 @ 1:06pm
by kornalius
mmtbb: First thing I notice is the way you access multi-dimensional array. You need to use the following syntax:

People$[1,1] and not People$[1][1].

This makes a big difference. The syntax you are using is only used for matrices, that is the reason why it is accepted by the parser in the first place. Since variable types are not known to the PPL compiler, the parser cannot give you any syntax error.

This should solve most of your problems.

PostPosted: Sep 6, 2005 @ 3:20pm
by mmtbb

PostPosted: Sep 6, 2005 @ 4:03pm
by kornalius

PostPosted: Sep 7, 2005 @ 6:13pm
by bmanske

PostPosted: Sep 7, 2005 @ 9:56pm
by kornalius

PostPosted: Sep 8, 2005 @ 3:55am
by bmanske

PostPosted: Sep 8, 2005 @ 12:56pm
by kornalius

PostPosted: Sep 8, 2005 @ 8:03pm
by kornalius
Brad,

The reason VarType() returns 1 and not 4 is because, the first element of the list is a string. It returns the type of the element you are on. I will change the VarType() to return the variable type and LType() to return the element types.

PostPosted: Sep 8, 2005 @ 10:08pm
by bmanske