Put some structure in your life!

Life can be a real succession of disorder sometimes. Don't let this way of life turn your programs into nightmares. Put some structure into your code. Variables are a great way to organize and store information, but you need to classify this information into clean and organized structures to be able to keep your code expandable for the future.

A typical variable can be declared and accessed quite easily in PPL. You declare its scope (local or global) if wanted and then you assign values into it. Nothing new here. What if you have a whole lot of information you want to store into variables? Are you going to create one variable for each value you need to store? If you have answered yes to this question, you need to read further as you will discover that structured variables will give you benefits you probably never considered.

Let’s pretend we need to write a simple three questions survey program. We need to gather user information first and then the user’s answers to three questions. You could do the following:

Name$ = "Alain Deschenes";
Address$ = "Somewhere somehow";
Tel$ = "555-555-5555";
Age$ = "32";
Occupation$ = "Too busy";

Question1$ = "His answer #1";
Question2$ = "His answer #2";
Question3$ = "His answer #3";

This will probably turn into a real nightmare when you reach 500 lines of code or more. What if you spell a variable wrong? What if you need to add user information and questions?

With structured variables (called structures), you can group a series of variables into what you might call categories. In our scenario here, we would need a user$ structure and a questions$ structure. Each structure will hold a series of variables.

struct(user$, "Name", "Address", "Tel", "Age", "Occupation");
struct(questions$, "Question1", "Question2", "Question3");

User.Name$ = "Alain Deschenes";
User.Address$ = "Somewhere somehow";
User.Tel$ = "555-555-5555";
User.Age$ = "32";
User.Occupation$ = "Too busy";

Questions.Question1$ = "His answer #1";
Questions.Question2$ = "His answer #2";
Questions.Question3$ = "His answer #3";

Yes it is longer to write but if you use this technique you are guaranteed to get great benefits in the long run.

You can declare as many structure elements as you want inside the Struct() function. Each element you declare is by default a double type variable that can hold pretty much any numerical value. However you can change the type of element you need. There are multiple variable types that PPL can support including:

TBYTE : 1 byte value. Range from 0 to 255.
TSHORT : 2 bytes value. Range from 0 to 65535.
TINT : 4 bytes value.
TUINT : 4 bytes unsigned value.
TDOUBLE : 8 bytes value. Support decimal point.

You can create a structure that will hold 4 bytes with 4 elements of 1 byte each.

struct(mystruct$, "a", tbyte, "b", tbyte, "c", tbyte, "d", tbyte);
mystruct.a$ = 1;
mystruct.b$ = 2;
mystruct.c$ = 3;
mystruct.d$ = 4;

You can also define a custom number of bytes the element will hold. You can then access each byte of the element variable using [x] array syntax.

struct(mystruct$, "element", 256);
mystruct.element$[34] = 20;

You can also copy one structure to another variable by doing the following:

newstruct$ = mystruct$;

You can also pass structures as parameters of funcs or procs like this:

proc MyProc (s$)
s.a$ = 10;
s.b$ = 20;
s.c$ = 30;
end;

proc main
struct(mystruct$, "a", "b", "c");
MyProc (&mystruct$);
ShowMessage(mystruct.a$ + ", " + mystruct.b$ + ", " + mystruct.c$);
end;