PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

2 Errors with arrays

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.
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
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.
Kornalius may have want to get the last word in on this, as he is the ultimate reference, but here is what I think is happening.

Code:
for(i$, 0, 11)
dd$ = "1,2,3";
strtolist(dd$, ",", tt$[i$]);
end;


I think that the StrToList function is turning tt$ into a list variable. (Kornalius will have to tell us what the compiler is doing with the [i$]) The list has 3 elements, so when you try to access the 4th you are past the end of the list.

Can I suggest...

Code:
#define InputStruc {"People$", "Size$", "Year"}

list (InputList$);
struct(InputElement$, InputStruc);
while (MoreDataToRead$)
// read data statements;
if (EndOfData$)
break;
dd$ = "1,2,3";
strtolist(dd$, ",", tt$);
add(InputList$);
struct(InputList$, InputStruc);
InputList.People$ = tt$[0];
InputList.Size$ = tt$[1];
InputList.Year$ = tt$[2];
end;


Then when you need to you just...

Code:
goto(InputList$, i$);
#object myclass theclass$(InputList.People$, InputList.Size$, InputList.Year$);


I use the goto function because I don't think that a struc will mix well with indexing into a list. Kornalius may tell me that I'm wrong and that it should work.

Hope this helps although it is not exactly what you started with.
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.
SizeOf() and Count() don't work.

THere needs to be a way to see the upper bound of an array
Hi,

Use VMAX(array$) to return the array's boundaries.
Kornalius,

I played with this problem a little as mmtbb presented it. His original code was much like this:

Code:
#include "console.ppl"

func WinMain;
InitConsole;
ShowConsole;

dim(tt$,12);
for (i$,0,11,)
writeln("read line "+i$);
dd$ = "1,2,3";
strtolist(dd$,",",tt$[i$]);
writeln(vartype(tt$)+", "+vartype(tt$[i$]));
end;

for(i$,0,11)
for (j$,0,2)
write(tt$[i$,j$]);
if (j$ != 2)
write(", ");
end;
end;
writeln("/");
end;
return(true);
end;


At about line 11 he is trying to insert a list into a single dimentioned array. This just doesn't look like it will work at all to me. The variable tt$ is redefined as a list variable with the boundry changed. I then added the varType statements to try to see what PPL was doing and I get a "1" every time. Shouldn't I see a "4" for tt$ after the call to StrToList?

mmtbb:

I changed the code a little bit and it works fine this way:

Code:
#include "console.ppl"

func WinMain;
InitConsole;
ShowConsole;

dim(tt$,12,3);
for (i$,0,11,)
writeln("read line "+i$);
dd$ = "S1,S2,S3";
strtolist(dd$,",",tmpList$);
for (j$,0,2)
tt$[i$,j$] = tmpList$[j$];
end;
end;

for(i$,0,11)
for (j$,0,2)
write(@tt$[i$,j$]);
if (j$ != 2)
write(", ");
end;
end;
writeln("");
end;
return(true);
end;


bmanske
I will take a look at this issue.

The problem is that you can`t have an array of list. You need to use a temporary variable. strtolist will redefine tt$ as a list variable.

I will see what I can do.
Kornalius,

I figured that it would be redefined. I'm not sure there is much to fix here with out redefining some aspect of the language. The only issue, as I see it, is why did tt$ not show as a list var when calling vartype.

Thanks for your help.

bmanske
Yeah you are right I cannot do much other than giving an error when the variable is an array type. I will take a look at this code sample and see what VarType() is returning and fix whatever has to be fixed.
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.
Thanks that will be much more understandable, for me at least.