简体   繁体   中英

loop through variables in a record structure in pascal

Is it possible for me to loop through variables in a record structure in pascal? for example

 Type = record
           tab_A : array_x
           tab_B : array_x
           tab_C : array_x
           .
           .
           tab_X : array_x

// is there a way to do this ? : 
for i:='A' to 'Z' do 
begin 
fillup(tab_i);   // to fill all the arrays without having to fill every single one manually
end;

(im sorry if the question isn't appropriate i couldn't find a better way to phrase it)

In the example you provide, you could declare

type TMyType = record
  tabs: array['A'..'Z'] of array_x;
end;

var 
  c: char;
  v: TMyType;
begin
  for c:= 'A' to 'Z' do 
  begin 
    fillup(v.tabs[c]);   // to fill all the arrays without having to fill every single one manually
  end;
  fillup(v.tabs['A']); // access one array in particular
end.

If you would like to give a specific name to each array, you can use the case syntax, along with packed to be sure there won't be a difference of alignment:

type TMyType = packed record
  case boolean of
  false: (tabs: packed array['A'..'Z'] of array_x);
  true: (tab_A, tab_B, tab_C, ..., tab_Z: array_x);
end;

var
  v: TMyType;
begin
  fillup(v.tabs['A']);
  fillup(v.tab_A); // same array
end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM