Natural SORT after FOR loop

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?

Can anyone clarify?

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).

steve

Just to clarify a statement in my last post;

Hence, what is the value of #i in the SORT and DISPLAY statements? Eleven, not ten, since the FOR loop increments, then tests.

This should say that the MAXIMUM value of #I (at least in the DISPLAY) would be 11.

steve

I guess the real reason for an NAT1316 is, that #i has a value of 11 after executing the for-loop.
Try this:

define data 
local 
1 #i(i2)
end-define
for #i = 1 to 10
  ignore
end-for
display #i

end

If you use a repeat-loop, you won’t get that error.

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 
* 
repeat until #i = #i-max
  add 1 to #i
end-all 
sort by #a(#i) using #i 
display #a(#i) #i 
end-sort

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

Page 1 12/21/07 09:13:27

#A #I #ASTRING


1 7 10 5 3 6 8 7 1 2 4 9 1
10 1 10 5 3 6 8 7 10 2 4 9 1
2 8 2 5 3 6 8 7 10 2 4 9 1
3 3 2 5 3 6 8 7 10 3 4 9 1
4 9 2 5 4 6 8 7 10 3 4 9 1
5 2 2 5 4 6 8 7 10 3 5 9 1
6 4 2 6 4 6 8 7 10 3 5 9 1
7 6 2 6 4 7 8 7 10 3 5 9 1
8 5 2 6 4 7 8 8 10 3 5 9 1
9 10 2 6 4 7 9 8 10 3 5 9 1

Among other questions you might want to answer, which would help explain the whole thing; is where did the asterisks in #a (11) go ???

steve

Now I realize: You’ll get a logical error. The last position is not displayed correctly…:shock:

 #A     #I
----- ------

1          7
10         1
2          8
3          3
4          9
5          2
6          4
7          6
8          5
1         10