Delphi for..in loop set enumeration order -
i want iterate through set of specific values. simple example below
program project1; {$apptype console} var a, b: word; wait: string; begin := 0; b in [1,5,10,20] begin := + 1; writeln('iteration = ', a, ', value = ', b); end; read(wait); end.
the sample code here expect , produces following
iteration = 1, value = 1
iteration = 2, value = 5
iteration = 3, value = 10
iteration = 4, value = 20
now if change order of set
b in [20,10,5,1]
the output same original, order of values not preserved.
what best way implement this?
sets not ordered containers. cannot change order of set's contents. for-in loop iterates through sets in numerical order.
if need ordered list of numbers, can use array or tlist<integer>
.
var numbers: array of word; begin setlength(numbers, 4); numbers[0] := 20; numbers[1] := 10; numbers[2] := 5; numbers[3] := 1; b in numbers begin inc(a); writeln('iteration = ', a, ', value = ', b); end; end.
Comments
Post a Comment