How to write this IF statement in a CASE in Progress 4GL? - if-statement

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?

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.

Related

Remove all text from string after a sequence of words in Scala

I am trying to assemble a UDF in Scala that takes a column from a data frame and manipulates it to remove HTML and other useless pieces of text.
The column I need to modify is very messy, sometimes there is HTML, sometimes there is not... Searching SO I have found a regex solution to remove HTML
what I'd like to accomplish now is to find a regex that can find a specific word in the text and delete all the text after that word.
I think I understand from this SO answer that the regex should be something like \).* if you want to remove all after ), so I am trying to adapt this to my case, unsuccessfully due to my lack of knowledge about regex.
I have strings like:
I am interested to hear from you, thanks Sent from iPhone other stuff I want to delete....
I'd like to retain the first part of the string up to "Sent from" excluded, so a perfect output would be:
I am interested to hear from you, thanks
What I have so far is something like:
val toStringNoHTML = udf[String, String](_.toString
// code from SO as linked above
.replaceAll("""<(?!\/?a(?=>|\s.*>))\/?.*?>""", " ")
// delete all text after key word
.replaceAll("""'Sent from'.*""", "")
// remove all punctuation
.replaceAll("""[\p{Punct}\n]""", " ")
)
While the HTML gets remove, the "Sent from" and all the text after does not. Any hint how to adjust the regex to make it work?
EDIT
as pointed out in the comment, a small typo prevented my code to work, thanks for the help:
.replaceAll("""'Sent from'.*""", "")
should be
.replaceAll("""Sent from.*""", "")
Instead of doing multiple replaceAll(pattern, blank) I'd be tempted to start with an extraction.
val msgRE = "(.*>)?(.*)Sent from.*".r
val result = udfStr match {
case msgRE(_, msg) => Some(msg.trim) // .replaceAll() can be added here
case _ => None
}
Here the result is an Option[String] but that really depends on how you want to handle the non-matching input.
If more cleaning is needed after the extraction then replaceAll() can be added where indicated (or the extraction pattern can be better refined).

Select a word by cursor and get its position in Qt/C++

I have a small Qt/C++ application with a QTextEdit which uploads some text. I want to be able to select a separate word by cursor and to get its position in the text.
For example, in the following sentence: "It is a sunny day".
If I select the word 'sunny', I will get the int 4, as it is placed at the 4th position in the sentence.
How can I achieve that?
You'll have to :
Get the field's text as QString using QTextEdit::text() method
Use QTextEdit.textCursor()->selectionEnd() to know where selection ends.
Use QString::mid to get the substring from 0 to end of selection
Use QString::count to know how many spaces it contains. This will give you access to the word's position.
Something like:
textEdit.text().mid( 0, textEdit.textCursor()->selectionEnd() ).count( ' ' )+1;
Hope it helps. That's minimal, you'll probably want to deal with partial word selection or any others relevant corner case.

QlineEdit with Japanese characters

I have a main widget in my application when I type something I display a child widget with lineEdit in it.If the input language is Japanese and I press any key then widget with line edit in it will appear with first character typed (lineEdit->setText("h")) and if I continue typing the next character should also appear. Now the issue which I am facing is that how to make the second character really "part" of the first character.
First letter is "h"
second letter is "e".
CASE 1 : When "h" is not part of "e" ,it will appear like" h エ".
CASE 2 : When h and e are combined in Japanese it looks like "ヘ".
I want to achieve case 2.
What I have tried:
Enabled the setAttribute( Qt::WA_InputMethodEnabled, true ); on my lineEdit
and also captured the QEvent::InputMethod but then I am not able to combine the first letter and subsequent letters user types.

Matching Specific Filenames

I can't figure this exercise at RegexOne.come: http://regexone.com/example/4?
This was my solution: (^.*\.(jpg|png|gif)$)
The capture text shows up green, but my result is all green checks except for the 3 that I need.
I tried this too: (^.*(\.jpg|\.png|\.gif)$)
Try this:
(^.*)\.(jpg|png|gif)$

Applescript and "starts with" operator

Is there a way to check (in applescript) if a list (or block of html text) starts with any number of values.
Example (checking for a single value)
if {foobar starts with "<p>"} then
-- do something awesome here
end if
except i would like to pass multiple values to check <p> or <h1> or <em>.
Thanks in advance.
on startswith(txt, l)
repeat with v in l
if txt starts with v then return true
end repeat
false
end startswith
startswith("abc", {"a", "d", "e"}) -- true
If you want to stay within the 'English' style of AppleScript, although longer than the example above, you can just do this:
if {foobar starts with "hello" or foobar starts with "goodbye"} then
A full example would be:
set foobar to "hello dude"
if {foobar starts with "hello" or foobar starts with "goodbye"} then
display dialog "found"
end if
That will be true even if you change:
set foobar to "hello dude"
to:
set foobar to "goodbye dude"