Jump to content
Macro Express Forums

"If Variable Contains" with multiple integers

Recommended Posts

Hello everyone!

So, odd issue and I'm unsure what I'm missing. When using the "If Variable" and running it as "if variable contains" to check a generic integer variable I'm confused if/how I can check said generic variable for any one of multiple numbers. Is there some syntax I'm missing here? Scenario is as follows: prompts allow user to define two different integers, they get added together,  resulting number is compared against a table to return a month as a text message box. So for example in my table my January is assigned the numbers of 01, 13, 25, 37, 49, 61, 73, 85, 97. Can I, using one "if variable" statement, check to see if my generic variable has any ONE of those numbers and to return the message as a result?

Thanks for the help! It's very much appreciated.

Link to comment
Share on other sites

Try something like this. The script tests whether the integer variable contains 01 and 99:


Variable Set Integer %Value%: Prompt
If Variable %Value% Contains "01"
If Variable %Value% Contains "99"
  Text Box Display: Yes, %Value% contains "01" and "99"
  Text Box Display: No, %Value% does NOT contain "01" and "99"
End If

Link to comment
Share on other sites

Can I, using one "if variable" statement, check to see if my generic variable has any ONE of those numbers and to return the message as a result?

The short answer is no, you can’t.
The longer answer is, take a look at SWITCH / CASE in the Help section.  Still a lot of lines to code, but it’s more readable than a bunch of IF / OR / IF / OR … commands.  

How about dividing your result integer by 12, then checking the remainder?  You would still need an IF or CASE for each month, but not half a dozen or so for each month.  

As far as I know, Macro Express does not just give you the remainder when you divide integers, so you will have to divide by 12 then multiply the result by 12 and subtract from the original.  Or code a loop to subtract 12 over and over until the result is less than 12.  If the data from the user is text you will have to convert to integer (Variable Modify Text) before doing the math. 

Probably easiest of all: convert text string to decimal, divide by 12, and check the decimal portion of the quotient to determine the month -- 0.08 is January, 0.16 February, 0.25 March, and so on.   


Link to comment
Share on other sites

Thank you both for the suggestions! I figured out a solution that while it's inelegant it's at least functional and relatively smooth. :) rberq, I will look into the ideas you mentioned...I haven't started using the SWITCH / CASE at all yet so this is a great opportunity to learn! Thank you both so much, I'm glad I found an active forum for this program, y'all help immeasurably.


Link to comment
Share on other sites

Glad to be able to help.

If you can script something as complex as nested IF-THEN-ELSE statements, you can probably wrap your mind around SWITCH / CASE statements. The Macro Express Help on the topic is excellent.

There may be times when SWITCH / CASE statements make for the cleanest code, but much depends on the number of cases. Sometimes I opt for IF-THEN-ELSE when there are not too many. Judiciously adding white spaces between blocks of code (i.e., blank Comment statements) can help add clarity to what would otherwise be convoluted code!

Link to comment
Share on other sites

  • 2 weeks later...

When I have a series of integers I contain them in an array. MEP has almost none of the normal programming methods for arrays that proper programming languages have so I have macros that I use as subroutines. One of them is "Find Index". It sets an integer value that's scoped to the parent macro and iterates though the array and aborts when the value is found. The integer value is set to the index of the value in the array. If the value is zero, it's not found. If it's something more, that's the index. I also have ones like "Find Last", "Sort" and even a binary search for large sorted arrays. It's a simple way to manipulate and query arrays without cluttering up your macros and writing code twice. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...