Discussions related to Visual Prolog
Audun Tönnesen
Active Member
Posts: 36
Joined: 27 Apr 2000 23:01

If-then-else....again!

Unread post by Audun Tönnesen »

I have reviewed our June 2007-discussion, but I keep getting syntax errors (150) as indicated below:

Code: Select all

... ...     CTL_LST = [hvaSkjedde_ctl, mistenkt_skade_ctl,etiologi_ctl,skader_ctl,tiltak_ctl,rutiner_ctl],                       foreach CTL = list::getMember_nd(CTL_LST) do                         postAction({ :-                                             if CTL = skader_ctl,! then                                             CTL:insertText("ICD 10-diagnoser")                                              else                                                    if   CTL = etiologi_ctl,! then                                                         CTL:insertText("Beskriv tilbakenforliggende fallmekanisme")                                                   end if                                             else                                       %error BEFORE 'else'                                             CTL:insertText("Beskriv")                                             end if                                     %error BETWEEN 'end' and 'if'                         CTL:selectAll(),                         CTL:overType := true(),                         CTL:wrapMode := sciLexer_native::sc_wrap_word                            })                     end foreach.
I can see that this becomes an if-else-else - loop, but I've really tried many variations.
If I omit the second "else if-then-end if"-block, the code compiles.
Regards,
Audun.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

You lack a comma after the last end if

But the main problem is with the else at the comment %error BEFORE 'else': it does not fit into any if:

The first if already have an else (4 lines befor the error) and the second if is terminated in the line above.

If I indent the code more strictly it may be easier to see the problem:

Code: Select all

        if CTL = skader_ctl then             CTL:insertText("ICD 10-diagnoser")         else             if CTL = etiologi_ctl then                 CTL:insertText("Beskriv tilbakenforliggende fallmekanisme")             end if         else % else already meet at this level             CTL:insertText("Beskriv")         end if, % missing comma inserted here         CTL:selectAll(),         CTL:overType := true(),         CTL:wrapMode := sciLexer_native::sc_wrap_word
You should also notice that cut (!) is not necessary in those tests (as they are not nondeterministic). Furthermore from Vip7.5 such cut are never necessary, because if have an implicit cut.
Regards Thomas Linder Puls
PDC
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

By the way you may consider this code instead:

Code: Select all

        postAction({ :-             InitList = [                 tuple(hvaSkjedde_ctl, "Beskriv"),                 tuple(mistenkt_skade_ctl, "Beskriv"),                 tuple(etiologi_ctl, "Beskriv tilbakenforliggende fallmekanisme"),                 tuple(skader_ctl,"ICD 10-diagnoser"),                 tuple(tiltak_ctl, "Beskriv"),                 tuple(rutiner_ctl, "Beskriv")             ],             foreach tuple(CTL, Text) = list::getMember_nd(InitList) do                 CTL:insertText(Text),                 CTL:selectAll(),                 CTL:overType := true,                 CTL:wrapMode := sciLexer_native::sc_wrap_word             end foreach         })
It seems clearer and the code becomes simpler when the controls are explicitly paired with their initialization text.

I have also moved the entire initialization code inside the postAction, i.e. postiong one action that does it all (instead of a lot of actions that each initialize one control). This is not impotant, but it seems "cleaner" to me.

However when the "action" becomes that complex it may be even better to extract it into a seperate predicate:

Code: Select all

        ...         postAction(initializeControls).   predicates     initializeControls : (). clauses     initializeControls() :-         InitList = [             tuple(hvaSkjedde_ctl, "Beskriv"),             tuple(mistenkt_skade_ctl, "Beskriv"),             tuple(etiologi_ctl, "Beskriv tilbakenforliggende fallmekanisme"),             tuple(skader_ctl,"ICD 10-diagnoser"),             tuple(tiltak_ctl, "Beskriv"),             tuple(rutiner_ctl, "Beskriv")         ],         foreach tuple(CTL, Text) = list::getMember_nd(InitList) do             CTL:insertText(Text),             CTL:selectAll(),             CTL:overType := true,             CTL:wrapMode := sciLexer_native::sc_wrap_word         end foreach.
Regards Thomas Linder Puls
PDC
Audun Tönnesen
Active Member
Posts: 36
Joined: 27 Apr 2000 23:01

Unread post by Audun Tönnesen »

I'm very grateful for private tuition!
Those if's and else's were veird, so I opted for your initialControls-suggestion. Much cleaner.

I haven't programmed since 2007 (VIP 7.1), so I realize it's time to study the new features. I'll better start with the core class! javascript:emoticon(':lol:')
Regards,
Audun.
Post Reply