Defining Arrays

Top 

Example code : How to define array I

type

   TDays = array[1..7of string;

const

   Days : TDays = ('Mon','Tue','Wed','Thu','Fri','Sat','Sun');

 

 

procedure TForm1.W3Button2Click(Sender: TObject);

var

   i : Integer;

Begin

   for i := 1 to 5 do      // Show the weekdays

     ShowMessage(Format('Day %d = %s',[i,Days[i]]));

End;

Result is:

------------------------

Day 1 = Mon

Day 2 = Tue

Day 3 = Wed

Day 4 = Thu

Day 5 = Fri

------------------------

Note: The Days array above was defined with a fixed 1..7 dimension. Such an array is indexable by values 1 to 7.

 

JS output:

  var Days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];

 

function W3Button2Click(Self, Sender$5) {

      var i$3 = 0;

      for(i$3 = 1;i$3<=5;i$3++) {

         alert(("Day "+i$3.toString()+" = "+Days[$Idx(i$3,1,7,"")].toString()));

      }

}

 

function $Idx(i,l,h,z) {

       if (i<l) throw Exception.Create($New(Exception),

               "Lower bound exceeded! Index "+i.toString()+z);

       if (i>h) throw Exception.Create($New(Exception),

               "Upper bound exceeded! Index "+i.toString()+z);

       return i-l;

};

 

Example code : How to define array II

procedure TForm1.W3Button2Click(Sender: TObject);

const

   Days : array[1..7] of string = ('Mon','Tue','Wed','Thu','Fri','Sat','Sun');

var

   i : Integer;

Begin

   for i := 1 to 5 do      // Show the weekdays

     ShowMessage(Format('Day %d = %s',[i,Days[i]]));

End;

Result is:

------------------------

Day 1 = Mon

Day 2 = Tue

Day 3 = Wed

Day 4 = Thu

Day 5 = Fri

------------------------

 

JS output:

function W3Button2Click(Self, Sender$5) {

      var i$3 = 0;

      var Days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];

      for(i$3 = 1;i$3<=5;i$3++) {

         alert(("Day "+i$3.toString()+" = "+Days[$Idx(i$3,1,7,"")].toString()));

      }

 

function $Idx(i,l,h,z) {

       if (i<l) throw Exception.Create($New(Exception),

               "Lower bound exceeded! Index "+i.toString()+z);

       if (i>h) throw Exception.Create($New(Exception),

               "Upper bound exceeded! Index "+i.toString()+z);

       return i-l;

};