Pointers

Any object in Gsharp can be specified using a string dataset rather than being hard-coded. Replace the hard-coded object with the name of the string dataset preceded by a $. e.g.

PTR = "page_1.viewport"+i;
create Viewport $PTR
 ( XuNxRatio = 1
);

It is possible to use pointers to specify part of an object e.g.

PAGE = current_page();
create Viewport $(PAGE).viewport_1;

The string dataset can be placed within parenthesis to clarify what is pointer and what is object e.g.

create Viewport $(WORK.CurrentPage).viewport_1

Don't Use String Arrays as Pointers

It is not possible to use an element of string array as a pointer. You must first assign the element to a scalar string. Instead of

POINTERS = "WORK.T"+(1:5);
for i = 1 to 5
  $(POINTERS[i]) = rnd(10);
endfor

use something like:

POINTERS = "WORK.T"+(1:5);
for i = 1 to 5
  PTR = POINTERS[i];
  $PTR = rnd(10);
endfor

or if possible:

POINTERS = "WORK.T"+(1:5);
for PTR in POINTERS;
  $PTR = rnd(10);
endfor

An Example

It is only necessary to use pointers to reference an object - pointers are not needed to reference strings. For example if we wanted to create 5 bar graphs of T1 to T5. First we would make one bar chart in Gsharp and then generate GSL:

create Viewport page_1.viewport_1;
create Domain page_1.viewport_1.domain_1;
create Graph page_1.viewport_1.domain_1.graph_1
   ( XuNgraphType = "bar",
     XuNyData = "T1"
   );

In order to make five graphs we put the create Graph command into a for loop:

create Viewport page_1.viewport_1;
create Domain page_1.viewport_1.domain_1;
for i = 1 to 5
  create Graph page_1.viewport_1.domain_1.graph_1
   ( XuNgraphType = "bar",
     XuNyData = "T1"
   );
endfor

But in order for this to work we must generalise the name of the object and the Y Data:

create Viewport page_1.viewport_1;
create Domain page_1.viewport_1.domain_1;
for i = 1 to 5
  PTR = "page_1.viewport_1.domain_1.graph_"+i
  create Graph $PTR
   ( XuNgraphType = "bar",
     XuNyData = "T"+i
   );
endfor

The create Graph command uses an object and so we have had to use a pointer, but the Y Data is specified using a string and so we have not had to use a pointer. A common mistake is to try something like:

create Viewport page_1.viewport_1;
create Domain page_1.viewport_1.domain_1;
for i = 1 to 5
  PTR = "page_1.viewport_1.domain_1.graph_"+i
  DATA = "T"+i;
  create Graph $PTR
   ( XuNgraphType = "bar",
     XuNyData = $DATA           #This is not correct. 
   );
endfor

If you wanted to avoid littering your DataManager with loop variables and pointers then put your code into a function and use local variables. Local variables are also quicker to use. You could also generalise your code so that it can be used again and again:

function CreateBarCharts(string VIEW, string DATASETS)
  float i;
  string PTR;
 
  create Viewport $VIEW;
  create Domain $(VIEW).domain_1;
  for i = 1 to size(DATASETS)
    PTR = VIEW+".domain_1.graph_"+i
    create Graph $PTR
     ( XuNgraphType = "bar",
       XuNyData = DATASETS[i]
     );
  endfor
endfunction

CreateBarCharts("page_1.view1", "T"+(1:5));