|
Post by milfredo on Oct 8, 2020 21:13:44 GMT -5
I have a nested Case statement. The Case Valuecache number is right for the dist. And the number for the finaltime valuevache fits in the case statement, but doesn't then give a rating. Just skips over and continues on. The finaltime is 72.95 And when it fits the condition it does not give rating of 17, just skips over. What am I doing wrong or what could be causing this?
Select Case dist
case 1320 'distance 6 furlongs
Select case finaltime
case (finaltime > 71.3) and (finaltime < 71.9 ) rating = 16
case (finaltime > 71.9) and (finaltime < 72.5)
rating = 17
case (finaltime > 72.5) and (finaltime < 73.1)
rating = 18
end Select
end Select
|
|
|
Post by tsh73 on Oct 9, 2020 1:10:54 GMT -5
remove "finaltime" in line
Select case finaltime
If your case statement compares with constants - it needs variable name in Select case But if you have a logical condition in case, it should not use variable - condition could be anything, of any variable.
( what happened in your code now: it evaluated condition (finaltime > 72.5) and (finaltime < 73.1) got 1 and proceed to compare finaltime with it - of cource it failed. )
|
|
|
Post by sarmednafi on Oct 9, 2020 1:53:00 GMT -5
Look Anatoly, Look to the following code please,
Select Case finaltime
finaltime = 1320 'distance 6 furlongs
Select case finaltime
case (finaltime > 71.3) and (finaltime < 71.9)
rating = 16
case (finaltime > 71.9) and (finaltime < 72.5)
rating = 17
case (finaltime > 72.5) and (finaltime < 73.1)
rating = 18
' end Select
print rating
end Select
wait
we disabling END SELECT LB Do not determined that. Please let Carl Gundel Knows about this. to fix it.
Thank you
|
|
|
Post by Chris Iverson on Oct 9, 2020 2:21:12 GMT -5
You misunderstood what he was saying. He wasn't saying to disable the END SELECT. He was saying remove the "final time" variable from the second SELECT CASE statement.
finaltime = 72.95 dist = 1320
Select Case dist case 1320 'distance 6 furlongs Select case 'finaltime case (finaltime > 71.3) and (finaltime < 71.9) rating = 16 case (finaltime > 71.9) and (finaltime < 72.5) rating = 17 case (finaltime > 72.5) and (finaltime < 73.1) rating = 18 end Select end Select
print rating
EDIT: Nevermind, I see what you mean. LB doesn't error out at removing the END SELECT in your code.
Strangely, it DOES error out when I do something that should be syntactically identical:
finaltime = 1320
Select Case finaltime case 1320 'distance 6 furlongs Select case finaltime case (finaltime > 71.3) and (finaltime < 71.9) rating = 16 case (finaltime > 71.9) and (finaltime < 72.5) rating = 17 case (finaltime > 72.5) and (finaltime < 73.1) rating = 18 'end Select end Select
print rating
EDIT2: Okay, I see the difference. That's what happens when you have a SELECT CASE statement with no CASEs AND no END SELECT. I agree this should be an error.
finaltime = 72.95 dist = 1320
Select Case dist 'case 1320 'distance 6 furlongs Select case 'finaltime case (finaltime > 71.3) and (finaltime < 71.9) rating = 16 case (finaltime > 71.9) and (finaltime < 72.5) rating = 17 case (finaltime > 72.5) and (finaltime < 73.1) rating = 18 'end Select end Select
print rating
select case test
|
|
|
Post by milfredo on Oct 9, 2020 20:15:40 GMT -5
I don't understand. I used a snippet of code for my post. The dist select statement has 8 different distances. Each distance then has a Finaltime case statement.
In main body, finaltimT1, FinaltimeT2, finaltimeT3 up to finaltimeT10. Each finaltimeT is assigned to finaltime variable and then sub routine sends it to the [checkrating] where the case statements are. I just used the code I posted for brevity. So why isn't it getting rating when condition is met as in my example? This is not my first time coding Select Case statements. That's why I am so confused.
Thanks
|
|
|
Post by Chris Iverson on Oct 9, 2020 22:33:49 GMT -5
Anatoly gave the answer. It's because you're specifying both a comparison variable AND using compound conditions. You can't do that.
Look at the first snippet in my post again.
finaltime = 72.95 dist = 1320
Select Case dist case 1320 'distance 6 furlongs Select case 'finaltime case (finaltime > 71.3) and (finaltime < 71.9) rating = 16 case (finaltime > 71.9) and (finaltime < 72.5) rating = 17 case (finaltime > 72.5) and (finaltime < 73.1) rating = 18 end Select end Select
print rating
That should behave how you expect.
Note that the finaltime variable is COMMENTED OUT in the second SELECT CASE.
The reason for this is that the SELECT CASE statement will automatically compare the comparison variable against the RESULTS of the conditions in the case statement.
Here's what I mean.
If you have a comparison variable:
SELECT CASE <comparison> CASE <condition> <result> END SELECT
This generates code equivalent to the following:
test = <condition> if <comparison> = test then <result> end if
As an example with live code:
comparison = 3 select case comparison case 3 print "three" end select
is equivalent to:
comparison = 3 test = 3 if comparison = test then print "three" end if
This is very important to understand, because this ALSO happens if you've specified compound conditions!
Let's try one.
comparison = 5 select case comparison case (comparison > 4) and (comparison < 6) print "five" end select
Let's convert this into the equivalent if statement, using the same template I specified earlier.
comparison = 5 test = (comparison > 4) and (comparison < 6) if comparison = test then print "five" end if
See the problem?
The boolean result of the condition(will only ever be a 0 or 1) gets stored in the test variable, and it's THAT result that gets compared against your comparison value. That's not what you want! You want the result of the condition itself to be checked, not to have that result compared with your specified variable!
To do that, leave out the comparison value, and the SELECT CASE will choose the case whose condition is true.
If you want to think of it in terms of the template specified earlier, think of SELECT CASE defaulting to a comparison value of 1, if one is not specified.
Following the template, this:
value = 5 select case case (value > 4) and (value < 6) print "five" end select
Evaluates to this:
value = 5 comparison = 1 test = (value > 4) and (value < 6) if comparison = test then print "five" end if
|
|
|
Post by milfredo on Oct 9, 2020 23:38:34 GMT -5
Thank you guys so much. I finally understand. I fixed the problem and it's working just fine.
You guys are the best!
|
|
|
Post by Carl Gundel on Oct 10, 2020 8:44:59 GMT -5
Anatoly gave the answer. It's because you're specifying both a comparison variable AND using compound conditions. You can't do that. So, is there a bug in LB or not?
|
|
|
Post by tsh73 on Oct 10, 2020 12:06:37 GMT -5
print 1 select case
print 2
prints
1 2
Shouldn't it btreak during compile?
|
|
|
Post by sarmednafi on Oct 10, 2020 23:52:17 GMT -5
Dear Carl,
Please made the new editor add END SELECT automatically because LB doesn't realize END SELECT is missing when I forgot it one time and LB keeps executing the code. Thank you for visiting the posts.
Regards' Sarmed Nafi.
|
|
|
Post by Chris Iverson on Oct 11, 2020 0:39:05 GMT -5
Anatoly gave the answer. It's because you're specifying both a comparison variable AND using compound conditions. You can't do that. So, is there a bug in LB or not? I would say yes and no The problem from the original post was a syntax misunderstanding, which is not a bug in LB, although perhaps some sort of compile warning might be useful to avoid this in the future? The difference between "SELECT CASE" and "SELECT CASE <comparison>" isn't something that's intuitive to new programmers, which may cause them to run into this problem the same way milfredo did. However, I would argue that what sarmednafi brought up IS a bug in LB. A SELECT CASE statement should not be able to exist without a CASE and an END SELECT. Such code should rightfully be considered a syntax error.
|
|