3

I'm trying to give the user the opportunity to filter on an image. In my code I will receive a string like

ASSIGN img = "images\green.png". 

I think it would be best to write the following IF statement in a CASE, but I don't know how, since you can not use something like MATCHES or any other variable value in a CASE statement.

IF img MATCHES "*green*" THEN DO:
    MESSAGE "It's green!"
        VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
ELSE IF img MATCHES "*orange*" THEN DO:
    MESSAGE "It's orange!"
        VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.
ELSE IF img MATCHES "*red*" THEN DO:
    MESSAGE "It's red!"
        VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.

So I obviously can't write it like this:

CASE img:
    WHEN MATCHES "*green*" THEN DO:
        MESSAGE "It's green!"
            VIEW-AS ALERT-BOX INFO BUTTONS OK.
    END.
    WHEN MATCHES "*orange*" THEN DO:
        MESSAGE "It's red!"
            VIEW-AS ALERT-BOX INFO BUTTONS OK.
    END
    WHEN MATCHES "*red*" THEN DO:
        MESSAGE "It's red!"
            VIEW-AS ALERT-BOX INFO BUTTONS OK.
    END.
END CASE.

Any ideas or do I just leave it at the IF statement?

  • I'm not clear what you are actually trying to do. The if-else-if tree is likely to be just as quick as a case statement, but agreed it's not so elegant. I understand your example is a trivial one, but in order to think through a more elegant way, I would need to understand what you're going to do. You mention filtering, but I can't see how the code you've shown would perform a filtering operation. – Screwtape Feb 23 '16 at 09:51
  • This code is just an example of the IF statement and doesn't show the code necessary for the filter. But I will just stick with the IF statement because it's easier to read. Thanks for your time! – David De Kok Feb 23 '16 at 12:33

1 Answers1

2

There is a small trick here that we use to put expressions in case statements:

CASE TRUE:
     WHEN img MATCHES "*green*" THEN DO:
          MESSAGE "It's green!"
             VIEW-AS ALERT-BOX INFO BUTTONS OK.
     END.
     WHEN img MATCHES "*orange*" THEN DO:
          MESSAGE "It's orange!"
             VIEW-AS ALERT-BOX INFO BUTTONS OK.
     END.
     WHEN img MATCHES "*red*" THEN DO:
          MESSAGE "It's red!"
             VIEW-AS ALERT-BOX INFO BUTTONS OK.
     END.
     OTHERWISE DO:
          MESSAGE "It's something else!"
             VIEW-AS ALERT-BOX INFO BUTTONS OK.
     END.
END CASE.
bupereira
  • 1,463
  • 8
  • 13