How to Select Multiple Items From the List and Calculate Values

I am using this list to generate both a string of text including different options separated by comma AND generate a total value of chosen items based on the Grade. I can now do either, but not both.

I find that I can either select multiple values and place them as text strings separated by comma (current set-up) or preface each one of the items in the list with a number (Grade) followed by two underscores so that the return value will be numerical, but not both.

How can it be solved?


EREFS.kmmacros (4.1 KB)

Don't "Search and Replace" the variable -- instead, use a "For Each line in EREFS_List" to process the chosen values one by one, building both your "text" and "sum" variables as you go.

If you can guarantee the format of "Some text: Grade <one-or-more-numbers> - More text" then the regular expression to use in your "For Each line" loop would be .*?: Grade (\d)+ -.* (which will allow you to go beyond single-figure grades).

Example:
EREFS copy.kmmacros (5.2 KB)

Image

You could extract the values from each line using regular expressions, but if this is a static list consider continuing what you've done with the "friendly list" format and put both the grade number and text, separated by something unique, before the double-underscore. For example, item 1 would be

0$$Edema: Grade 0 - Distict Vascularity__Edema: Grade 0 - Distict Vascularity

You can then treat each line as an array and get the code with %Variable%eachLine[1]$$% and the text with %Variable%eachLine[2]$$%. That gives you a bit more flexibility with the format in the future because you're in control.

1 Like

Hey @sudelb,

Nigel seems to have this covered, but I have a couple of observations:

  • Don't use global variables, unless you have a real need. Somewhere, sometime they will bite you, since they are persistent.
     
  • Remember that the double underscore format changes what is displayed in the Prompt With List action.
    • Very Complicated Text__Very Simple Label
      • You'll see Very Simple Label, but what will be returned is Very Complicated Text.

-Chris

@ccstone - thank you for your suggestions - going to work to change all my macros variables to local :slight_smile:

@Nige_S

I get your point, but for some reason you macro example does not work:

  1. for some reason only some of the lines appear when macro is executed (not all) - see pic

  1. It generates multiple text displays instead of combining all the variables into one

  2. to explain my point further - see selections on pic 2.

I'd want the text variable produce:

Edema: Grade 0 - Distict Vascularity, Exudate: Grade 1 - Mild (< 10% surface area), Furrows: Grade 0 - None, Rings (trachealization): Grade 0 - None, Stricture: Grade 0 - Absent

And the number variable produce the sum of all highlighted fields: 1

(0+1+0+0+0)

OK figured out an answer to my own first question - just had to delete 0 from default initial search string

Happy to report that my initial post contained a solution for a list of items selected as I wanted.

I however cannot figure out how to make the part below red pause to work to calculate the sum of all grades:

EREFS copy.kmmacros (7.0 KB)

Macro Image

Try this simplified version.

  • The example includes these features:
    • When it comes to inserting the text I recommend doing this by pasting instead of typing – unless there's a real need for typing.
    • You can also delete the past clipboard.
    • Macro presently displays the results – pasting is disabled.

EREFS.ccs v1.00.kmmacros (11 KB)

1 Like

Appreciate your help, @ccstone.

This is exactly what I was trying to achieve!

The reason I use type instead of paste, is that most of my work macros are to supplement poorly designed electronic medical records software which I access via Citrix, and for some reason I found that "paste" does not always work reliably on Citrix as opposed to "type" even though this is not my preference and takes longer to execute.

Glad to hear it.

Ah, I get it.

In the new macro take note of:

  • The importance of the order of operations.

  • The simplicity of the final regular expression.

    • In KM this required the use of a loop to search each line, although there is a way to loop through all pattern matches instead – but it's more complicated.
    • In many scripting/programming languages the search would be a single line of code, and people get a bit confused by KM's way of doing things.

Ultimately the macro turned out to be a bit simpler than you thought. As Einstein said: "Make it (whatever it is) as simple as possible (as is reasonable) but no simpler."

Take Care,
Chris

1 Like

I see you've sorted that out for yourself. Note that your original macro's "Prompt..." action has a "Default" of "0", which is why you get that initial filtering -- if you don't want it, leave "Default" blank.

Because I'm evil and was only showing you how to extract the values from each line, making you work out how to combine them. Chris is much nicer than me :wink:

Looping and adding, and looping and appending, to a variable are common operations and it's good to get comfortable with them by trying different ways of doing it for yourself.

1 Like

Thank you @Nige_S