Why, when I invoke a natural sort after a FOR loop do I get a 1316 error?
Code:
define data
local
1 #a(a5/1:10) init<‘10’,‘5’,‘3’,‘6’,‘8’,‘7’,‘1’,‘2’,‘4’,‘9’>
1 #i(i2)
1 #i-max(i2) init <10>
end-define
*
for #i = 1 to #i-max
if #i = 10
write ’ ’
end-if
end-all
sort by #a(#i) using #i
display #a(#i) #i
end-sort
end
it appears that the sort needs an extra array value to do the sort, as if I up to 11, it works?
Hi Cogs,
I think the problem is basically somewhere else !
With the natural sort you build up a “temporary DB” in memory (- or filesystem)
and then read it back. (It’s NOT a bublesort of your own array;-)
The fields for this (the ones that are “written” to memory) are an A5 and an I2 field!
I would suggest you make a temp variable like below.
Finn
DEFINE DATA
LOCAL
1 #A(A5/1:10) INIT<‘10’,‘5’,‘3’,‘6’,‘8’,‘7’,‘1’,‘2’,‘4’,‘9’>
1 #B (A5)
1 #I(I2)
1 #I-MAX(I2) INIT <10>
END-DEFINE
*
FOR #I = 1 TO #I-MAX
IF #I = 10
WRITE ’ ’
END-IF
MOVE #A (#I) TO #B
END-ALL
SORT BY #B USING #I
DISPLAY #B#I
END-SORT
END
First, as will be evident if you run the code, Finn’s code is the proper way to do this.
As to why your code has a problem; play Natural. Where does the FOR loop end? Your code indicates you think it ends at the end-sort; not true, it ends at end-all.
Hence, what is the value of #i in the SORT and DISPLAY statements? Eleven, not ten, since the FOR loop increments, then tests.
If you were to make the array A5/1:11, with some sort of dummy value in the eleventh entry, the code actually runs. (but, on the PC, 10 comes after 1).
What is fascinating about this little program is what is really going on. If you wish to scratch your head a bit; see how long it takes for you to satisfactorily explain the following code and output to yourself:
DEFINE DATA
LOCAL
1 #A(A5/1:11) INIT<‘10’,‘5’,‘3’,‘6’,‘8’,‘7’,‘1’,‘2’,‘4’,‘9’,‘**’>
1 #I(I2) INIT <1>
1 #I-MAX(I2) INIT <10>
1 #ASTRING (A30)
END-DEFINE
*
INCLUDE AASETC
FOR #I = 1 TO #I-MAX
IGNORE
END-ALL
SORT BY #A(#I) USING #I
COMPRESS #A (*) INTO #ASTRING
DISPLAY #A(#I) #I 5X #ASTRING
END-SORT
END