# NoteTab - Calculating Text

In my latest foray into NoteTab arithmetic I came across a curious fact around performing arithmetic calculations on a variable containing text.  Whilst in purely mathematical terms an arithmetic operation on something that has a numeric value is a bit nonsensical, but NoteTab is a text editor…

``````H="Inc Text"
^!Set %Val%=FooBar
^!Prompt >^%Val%^%Val%<
``````

This clip displays two prompts when run.  The first prompt as one might expect displays “>FooBar<”.  The second prompt however displays “>1<”!

Apparently any text is evaluated to zero when you try and perform a purely arithmetic operation on it.  Which broadly speaking makes sense if you just think about a piece of text as having no arithmetic value (or “zero”).

So I came across this interesting point when I was taking a masked numeric input from a clip wizard.  Take a look at the following clip.

``````H="Strange Result"
^!Set %Val%=^?{(M="099")Enter a value=2}
^!Inc %Val%
^!Prompt ^%Val%
``````

Running this clip and accepting the default input of two you mjight expect should give you a final prompt of  “3”, except you’ve spotted where I’m going with this and you know that it’s probably going to be something to do with adding numbers to text and so turn out to be “1” instead.  Well … you’re absolutely right.

The result is 1 as the input mask (even though it is using optional numeric place holders after the first compulsory numeric place holder) simply pads the input value with spaces.

i.e.

The calculation should be of the form: 2+1=3

But we actually get: “2  “+1=0+1=1

The answer is therefore quite simple - remove the spaces…

``````H="Better Result"
^!Set %Val%=^\$StrTrim("^?{(M="099")Enter a value=2}")\$
^!Inc %Val%
^!Prompt ^%Val%
``````

So the astute amongst you will have noticed that we are now trimming the input to remove spaces which should now nicely produce the desired result when we use the default of 2 giving us a final result of 3.

Errr…. except we still get a result of 1.

It seems that there’s either a bit of a bug or an evaluation order at work here that means we don’t actually end up trimming off the spaces.  It seems we need an extra assignment step.  The following clip does produce the desired result of 3.

``````H="Right Result"
^!Set %Val%=^?{(M="099")Enter a value=2}
^!Set %Val%=^\$StrTrim("^%Val%")\$
^!Inc %Val%
^!Prompt ^%Val%
``````

So the moral of the story is ensure that your variables are purely numeric before you start performing arithmetic operations on them and make sure that if you need to trim user input for a numeric you assign it to a variable first before trying to trim.

Author: Stephen Millard
Tags: | notetab |

• ## NoteTab Clip - Day of Week

19 Feb 2009

I seem to be doing rather a lot with NoteTab at the moment and again I thought I’d share something useful I scripted. This time around, it’s relating to date/time and days of the week.

• ## NoteTab Numeric Accuracy

15 Feb 2009

I’ve been working on something to help me with some logging activity in NoteTab and I’ll be posting a variety of useful bits and pieces from this in both this micro blog (FlagIT) and my main blog (RebootIT).  As part of this I was looking at producing some date related clips which necessitated looking at some mathematical clips and notably one to determine if a number is an Integer.  NoteTab is a text editor and numeric manipulations are definitely not its forte, so I needed to produce a clip function to do this.

• ## NoteTab Clip - Duplicate File

02 Feb 2009

Quite often when working on a file I want to clone it as a backup or to work on another copy to try something out. I was doing this so often and being such a lazy coder I whipped up a quick NoteTab clip to make it even easier. Run it with the file you want to duplicate open in NoteTab and it will create a duplicate in a new tab and give it the focus.

• ## NoteTab Clipping - Binary Clip Wizard Options

01 Feb 2009

NoteTab clipping uses the clip wizard functionality to produce sophisticated input dialog windows to obtain information from the user.  There is however a strange view produced when looking at a single check box. The clip code shown below shows the various pieces of clip code used and below that are the discussions of the approach and options along with the screen shots of what the clip wizard produces.