简体   繁体   English

在 Pascal 中循环遍历记录结构中的变量

[英]loop through variables in a record structure in pascal

Is it possible for me to loop through variables in a record structure in pascal?我可以在 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:如果您想为每个数组指定一个特定的名称,您可以使用case语法和packed以确保不会有 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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