
count of not updated in a while loop
count of not updated in a while loop
Hi there,
I have this simple algorithm to implement:
1. Check how many working days there are between date x and date x - y days (y = 3);
NOTE: I have a special table listing all days till 2050: the day has a field (called FF) with an "F" if it is not a working day, if the FF is blank that day is a working day
2. If the working days are less than 4, increment y, otherwise stop
3. Check again the working days and in case increment y again.
That's should be pretty easy:
j := 3 .
--Counts how many working days between two dates
Wdays := count of Calendar named "tCount1" with day between ( jobs Delivery_date - j ) to jobs Delivery_date AND FF = blank .
while Wdays < 4 do
j := j + 1 .
Wdays := count of Calend named "tCount2" with day between ( jobs Delivery_date - j ) to jobs Delivery_date AND FF = blank .
end
The problem here is that the second "count of" never updates, it returns the same value again and again (I have checked with some message instructions and j is updated, but Wdays has always the same value), it seems that it retains the first result forever.
Could you please advise on this?
Regards
Re:count of not updated in a while loop
This is one of the 20 least well known "bugs" in DataEase, but it is KNOWN ;-)
There has been much debate on this as there is some that think it is correct and some that think it is not correct. Luckily we think it is not correct, so it is on a fix list.
Most likely this was done due to some "mistaken" optimization process, but anyhow...
The workaround is simple as the problem is that WHILE is not a relational command, and as it is NOT a relational command it does not trigger a recalculation of relational dependencies.
So to get it to work one need to take advantage of this "knowledge" and introduce a relational command in the equation.
We simply do that by creating a table called DUMMY with one row and one record.
In our sample the original DQL looks like this:
define "Retval" text .
define "howmany" number .
define "counter" number .
while Counter <3 do
counter := counter + 1 .
howmany := count of somedata with Key = counter .
list records
Counter ;
howmany .
end .
To make it simple we have 1 row with key 1, 2 rows with key 2 and 3 rows with key 3...
The result of the Normal While is like this:
Key: 1 HowMany? 1
Key: 2 HowMany? 1
Key: 3 HowMany? 1
Obviously wrong, as it only calculate the count of once (would be the same with any, sum of etc.)
So we change the DQL to look like this:
define "Retval" text .
define "howmany" number .
define "counter" number .
while Counter <3 do
counter := counter + 1 .
for Dummy ;
howmany := count of somedata with Key = counter .
end
list records
Counter ;
howmany .
end .
And then suddenly everything is Honkey Dorey...
Key: 1 HowMany? 1
Key: 2 HowMany? 2
Key: 3 HowMany? 3