Reply to topic  [ 6 posts ] 
How to test if a selection is a number or text? 
Author Message

Joined: 2007-04-12 14:59:36
Posts: 229
Using the *TypeOf" command produces "text" even for a numerical text selection.
How can one test if a selection is a number?


2013-12-23 03:36:13
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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.

_________________
philip


2013-12-23 20:32:35
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
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.


2013-12-25 09:03:27
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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.

_________________
philip


2013-12-25 18:25:34
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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.

_________________
philip


2013-12-25 18:44:29
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
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.


2013-12-26 11:24:33
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software