Is there a function for isInteger or similar? Also, Case of?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Is there a function for isInteger or similar? Also, Case of?

Post by Bone White »

Hello all,

I've searched the forums and the documentation wiki, but cannot find anywhere a function which checks if a value is an integer or not. As always, explaining my situation is the best way to get my problem resolved:

I have a single text input field, which I want the player to input a number, but the code requires it to be an integer. Ideally I would've preferred a Case..Of function to do this, but it appears this doesn't exist either (correct me if I'm wrong).

Pseudo code of my macro is:

Code: Select all

If input = -5 then
  perform ruleset -5 else;
...
If input = 0 then
  perform ruleset 0 else;
If input = 1 then
  perform ruleset 1 else;
If input = 2 then
  perform ruleset 2 else;
...
I can [abort()] the program where I choose, I've been studying the syntax and definitions a lot over the last two nights. However, the problem I get is if the input is something unwanted, such as 4.2 or -2.1 it ALWAYS runs ruleset 1 (which makes no sense, there's not even any rounding here.

1. Is the easiest/only way to put a the start a plethora of "If input >0 && <1 then abort(0)" for the distance between each integer?

2. Is there an isInteger function

3. Is there a Case..of function?


I have a secret project in the works, though whether I decide to dedicate time to it is another matter. It all hinges on the scope of the macro language.

Thankyou in advance.

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: Is there a function for isInteger or similar? Also, Case

Post by Rumble »

Don't believe there is an isInteger() function; there's an Wiki: isNumber() function and there are user-created "drop-in" macro functions including a math library. I've never used it, but it's out there somewhere.

I am not familiar with Case...Of - there is [switch:] roll option, but I don't think that's the same thing you're asking.

Edit: it seems that the switch roll option is what you need. It would be most helpful if you'd post the actual macro code, but if it's a secret project...

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Is there a function for isInteger or similar? Also, Case

Post by aliasmask »

You have a couple of options. You can either force invalid entries to abort the process, or you can process a default value, or you can ask again for a correct value, or you can try and correct the value. Another option is to instead of having the user manually enter a value, only provide a select list of values using Wiki: input() LIST option. I assume all users are 5 years old and I limit their responses when possible and I always assume they will enter incorrect data. I also try to provide detailed instructions if I think there a chance of misinterpretation or just plain ignorance.

isInt is easy enough to program for:

Code: Select all

[H, if(isNumber(number)): isInt = if(number == round(number),1,0); isInt = 0]

User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: Is there a function for isInteger or similar? Also, Case

Post by Bone White »

The code is exactly what I posted as it's pseudo, there's nothing more to it at the moment, just trying to get functionality of it down.

Case..Of comes from Delphi I think, and it works like this:

Case {Variable} Of
1: output The Variable is 1!
2: output The Variable is 2!
3..4: output the Variable is between 3 and 4
else output The Variable is neither 1, 2 or inclusively between 3 and 4.

An extremely useful function to have in a program language. I hope this helps explain.

Ah, after typing that out I actually read the article for switch, and it works exactly the same way as Case..Of

aliasmask, I don't want it to round, I want it to stop if it's not an integer.

I am now using:
Spoiler

Code: Select all

[h: input=input]
[h: abort(input)]
[h: output=0]
[switch(input), code:
case "1":
	{[output=1]
         Case 1!
	};
cast "2":
 	{[output=2]
         Case 2!
	};
default:
	{[abort(output)]
	}]

[r: output]

P.S. Does the site have an IRC channel or such to communicate? I have a steaming pile of questions which I don't particularly want to fill up an entire board with.

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: Is there a function for isInteger or similar? Also, Case

Post by neofax »

Rumble wrote:Don't believe there is an isInteger() function; there's an Wiki: isNumber() function and there are user-created "drop-in" macro functions including a math library. I've never used it, but it's out there somewhere.

I am not familiar with Case...Of - there is [switch:] roll option, but I don't think that's the same thing you're asking.

Edit: it seems that the switch roll option is what you need. It would be most helpful if you'd post the actual macro code, but if it's a secret project...
I was just replying that Switch is the same as Case, but you ninja'd me.

User avatar
aliasmask
RPTools Team
Posts: 9029
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Is there a function for isInteger or similar? Also, Case

Post by aliasmask »

Bone White wrote:aliasmask, I don't want it to round, I want it to stop if it's not an integer.
The code posted doesn't round. It just checks to see if the raw number equals the rounded number, if it does then it's an integer. isInt returns 1 or 0 for true or false, not the number. I think your confusion may come from the == which is a comparison operator, not an assignment operator.

The MT switch is a little less versatile with the inability to include multiple values for the case. I rarely go with the switch statement and just stick with a series of if statements, especially in cases where multiple values have a same result.

Here's an alternate way to handle groups of data based on number ranges. You probably don't need to do it this way because of your small set of numbers, but it may be an interesting read:

http://forums.rptools.net/viewtopic.php ... 24#p162934

User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: Is there a function for isInteger or similar? Also, Case

Post by Bone White »

Ah, arrays. I was wondering what on earth these json things are which I've been reading about. Trying to claw my way back into working practice as a coder after a decade absence.

I've noticed a peculiar function when using code "nests", making it appear to me that the macro is read until the code, then the code inside the nest is compiled, then the entire code is read through again. This caused me a big problem, see my previous post for the code.

When 0 is entered as input, the code aborts through default. I've confirmed this by replacing the abort line with a printline instead.

I fixed this rather long-windedly:
Spoiler

Code: Select all

[h: input=input]
[h: output=0] <!- to prevent those annoying popups I assume are caused by the same problem. ->
[h: input=input * 10 + 1]
[switch(input),code:
case "1": {[output="You chose 0"]};
default: {[abort(output)]}]
[r: output]
Of course in this instance I transform all my case numbers to the new computation. It works, though it's darn ugly. Can you please explain why I had such a problem with the first way?

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: Is there a function for isInteger or similar? Also, Case

Post by Rumble »

I don't know how macro code is actually read - it's all handled through regular expression parsing, which provides for its own host of idiosyncrasies. As far as the issue you mentioned in your prior code, if input is 0, then it will abort immediately - abort(input) sees to that:

Code: Select all

[h: input=input]
[h: abort(input)] <!--this line aborts if input = 0, which is expected functionality -->
[h: output=0]
[switch(input), code:
case "1":
   {[output=1]
         Case 1!
   };
cast "2":
   {[output=2]
         Case 2!
   };
default:
   {[abort(output)]
   }]

[r: output]
Is that the problem you're talking about? If so, it's simply the behavior of the abort() function - if the argument passed to it evaluates to 0, then all macro processing is aborted entirely. If you want to gather input via a dialog and abort the process if the user cancels, though, you can use the Wiki: input() function:

Code: Select all

[h: status = input("input|0|Enter Value")]
[h: abort(status)]
[h: output=0]
[switch(input), code:
case "1":
   {[output=1]
         Case 1!
   };
cast "2":
   {[output=2]
         Case 2!
   };
default:
   {[abort(output)]
   }]

[r: output]
In the above example, the variable "status" is assigned the return value of the input() function call. If the user hits OK, then that value is 1; if they cancel, the value is 0. So, hitting cancel aborts the rest of the macro (which is a good thing, because otherwise you'd get a lot of prompts for missing values if the user hit cancel, because the macro would keep running anyway if you don't put in some way to handle the cancel button).

User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: Is there a function for isInteger or similar? Also, Case

Post by Bone White »

I unfortunately edited the macro which had the problem so cannot post it. I removed the abort near the top, only leaving an abort in the default section like so:

Code: Select all

[h: input=input]
[h: output=0]
[switch(input), code:
case "0":
   {[output=0]
         Case 0!
   };
default:
   {[abort(output)]
   }]
yet I'd still get the code aborted on a 0 entry. For some reason a value of 0 would run through both case "0" and default. I assume this is because 0 is the default value? In the documentation wiki it says it would occur for all other cases of the input variable not specifically listed. I checked this was the case by replacing [abort(output)] with [test=output + " " + input] then printing it at the end. Sure enough, the output was Case 0 and the input was 0.

I may be missing a line which completely messed it up, as this seems highly irrational, this will teach me to constantly "improve" my macros and not leave evidence of past successes/failures to improve upon.

I appreciate the advice Rumble, and I am trying to get my head around your superb click target macros to sabotage err... adapt them for my own use. I was using a rather abbreviated version of the input function. input=input generates a dialogue box with a default value of 0.

I didn't want my box to close down on a 0 entry, that's why the mathematic transformation.

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: Is there a function for isInteger or similar? Also, Case

Post by Rumble »

Ah, okay, I think I see the issue - your code has a string vs. number mixup.

In this:

Code: Select all

[h: input=input]
[h: output=0]
[switch(input), code:
case "0":
   {[output=0]
         Case 0!
   };
default:
   {[abort(output)]
   }]
You're asking if input equals the string "0", rather than the number 0. Since it doesn't, that first case doesn't match, and it bails out to the default case, where you've sent the value 0 to abort, and, well, it aborts. If you do this:

Code: Select all

[h: input=input]
[h: output=0]
[switch(input), code:
case 0:
   {[output=0]
         Case 0!
   };
default:
   {[abort(output)]
   }]
it should work (I tested it, and it worked as expected) - note that the difference is subtle, but the first case is 0, not "0". The reason the other things worked were that by doing the string concatenation, you forced it to become a string.

User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: Is there a function for isInteger or similar? Also, Case

Post by Bone White »

Input automatically parses a number entry in an input box as a number, not a string? That was unexpected, and rather funny. I'm so used to defining the fields of every paramater, I assumed that a text input box would output "0" not 0.

User avatar
Rumble
Deity
Posts: 6235
Joined: Tue Jul 01, 2008 7:48 pm

Re: Is there a function for isInteger or similar? Also, Case

Post by Rumble »

Bone White wrote:Input automatically parses a number entry in an input box as a number, not a string? That was unexpected, and rather funny. I'm so used to defining the fields of every paramater, I assumed that a text input box would output "0" not 0.
Yep. If you put a number in the field, it will assume you really mean number; if you have anything except digits (and I believe + and - signs), it'll be handled as a string. One of the idiosyncrasies of the macro language, more or less. You get used to them pretty quickly. I guess in this case it's either due to the way Java handles input dialogs and fields, or it was the developer thinking "Well, rather than have two types of input fields, we'll make a good-faith guess at it and let the macro writer handle exceptions."

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Is there a function for isInteger or similar? Also, Case

Post by Azhrei »

Bone White wrote:P.S. Does the site have an IRC channel or such to communicate? I have a steaming pile of questions which I don't particularly want to fill up an entire board with.
No, we don't. It's an interesting idea, though.

For something a little more real-time, maybe we could do some kind of Google+ Circle? Folks here who felt like helping out could monitor the stream and post answers. The advantage is that it would be automatically indexed by Google which would be cool (;)) but I'm not sure it would be any faster -- I check this site when I check it and having a stream on G+ wouldn't change that for me. But then I'm not the one answering the macro questions!!

chaossdragon
Giant
Posts: 145
Joined: Tue Jul 05, 2011 10:18 pm

Re: Is there a function for isInteger or similar? Also, Case

Post by chaossdragon »

here lately I haven't had anything to do but check this site like every hour or so. really don't see much of a difference in a live chat vs forum post/reply
and the confusion factor of live chat would grow exponentially when you have several people posting code snippets concerning various functions/concepts.
unless it had a type of private chat option to keep the main window clean, in which case it would almost be better to just set up a server(by the questioner)
and client connect(by the helper/adviser), which is what I do when I have either a lot of questions, or just really confused.

Post Reply

Return to “Macros”