nisus.com
https://www.nisus.com/forum/

How to test if a selection is a number or text?
https://www.nisus.com/forum/viewtopic.php?f=17&t=5586
Page 1 of 1

Author:  js [ 2013-12-23 03:36:13 ]
Post subject:  How to test if a selection is a number or text?

Using the *TypeOf" command produces "text" even for a numerical text selection.
How can one test if a selection is a number?

Author:  phspaelti [ 2013-12-23 20:32:35 ]
Post subject:  Re: How to test if a selection is a number or text?

A (text) selection subtext is always going to be a text object. If you want to know whether the text is digits you could use find:
Code:
$sel.subtext.find '\d+', 'E'

(or something more carefully worked out :) )
But maybe you don't really need to test? You could just use Cast to Int or Cast to Float. If the text is a number you will get a number otherwise you will get 0. It would all depend on what you are trying to achieve.

Author:  js [ 2013-12-25 09:03:27 ]
Post subject:  Re: How to test if a selection is a number or text?

This produces only a reminder that "The Text object does not have a 'subtext' property".

And yes, I need the test. It serves to look up either a chapter references or text string within a file. The macro should do differently things depending on whether it's one or the other. The macro should be able to to know f.e. that if "12" is selected the selection is a number, and that if "Santaclaus" is selected the selection is a word. Nothing more and nothing less.

Author:  phspaelti [ 2013-12-25 18:25:34 ]
Post subject:  Re: How to test if a selection is a number or text?

js wrote:
This produces only a reminder that "The Text object does not have a 'subtext' property".

That just means that the variable you are using is already a text object (not a selection). So you can directly use .find.

js wrote:
And yes, I need the test. It serves to look up either a chapter references or text string within a file. The macro should do differently things depending on whether it's one or the other. The macro should be able to to know f.e. that if "12" is selected the selection is a number, and that if "Santaclaus" is selected the selection is a word. Nothing more and nothing less.


Code:
$sel = TextSelection.active
$selTxt = $sel.substring

if $selTxt.find('\d+', 'E')
   prompt 'I found a number'
else
   prompt 'No number found'
end


But as already said, depending on what you want you should formulate the find expression more carefully. This example will accept any selection that contains a digit somewhere.

Author:  phspaelti [ 2013-12-25 18:44:29 ]
Post subject:  Re: How to test if a selection is a number or text?

Anyhow in Nisus a selection (or more accurately, the subtext/substring of the selection) is always a text object, and so if you want to distinguish different selections, you should treat this as a text matching task.
The number/text distinction only comes into play, if you want to do calculations with it. But even in that case Nisus will just take care of the conversion (treating any input that does not look like a number as 0). Still it might be good form to use a Cast as command, before you do math with the selection.

Author:  js [ 2013-12-26 11:24:33 ]
Post subject:  Re: How to test if a selection is a number or text?

Thank you, this works very well for me. The problem was to look up text on one hand and chapter references on the other hand with one single macro. As chapter references text appear in different formats, like "5.1" or "5:01" or "5.01" for one and the same thing, I wanted to treat these numeric searches separately, to allow searches in any of these formats. No need to recasting for this.

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/