Re: Testing validity of numerical content of a string [message #51742] |
Wed, 06 December 2006 17:49  |
Brian Larsen
Messages: 270 Registered: June 2006
|
Senior Member |
|
|
On Dec 6, 6:38 pm, David Fanning <n...@dfanning.com> wrote:
> Y.T. writes:
>> Maybe this is a stupid question, but is there a way in IDL to tell
>> whether a string contains a valid representation of a particular data
>> type? For example an integer? I can do something like
>
One "unixy" way to do this is with regular expressions. I end up using
these all the time (any should more).
Play with something like:
IDL> a='hello'
IDL> print, stregex(a, '[a-zA-Z]', /boolean)
1
IDL> b='3.14159'
IDL> print, stregex(b, '[a-zA-Z]', /boolean)
0
And one could work out a better regexp that looked for other
non-numeric chars as well.
This is good and easy.
Cheers,
Brian
|
|
|
|
Re: Testing validity of numerical content of a string [message #51841 is a reply to message #51742] |
Wed, 06 December 2006 18:50  |
Brian Larsen
Messages: 270 Registered: June 2006
|
Senior Member |
|
|
I just read you post a little closer and the numbers you have are more
complicated but not too bad.
IDL> print, stregex('hello', '[a-df-zA-DF-Z]', /boolean)
1
IDL> print, stregex('3.14159', '[a-df-zA-DF-Z]', /boolean)
0
IDL> print, stregex('3.14159e3', '[a-df-zA-DF-Z]', /boolean)
0
To make it work perfectly you would need to know a little more info
about what could be there but if's based on these regular expressions
can tell the difference between 'hello' and '-.3e-4' which can then
build valid_float.pro.
When I can't remember the syntax I always grab my sed & awk book but
there are plenty of online references as well.
Cheers,
Brian
On Dec 6, 6:49 pm, "Brian Larsen" <balar...@gmail.com> wrote:
> On Dec 6, 6:38 pm, David Fanning <n...@dfanning.com> wrote:
>
>> Y.T. writes:
>>> Maybe this is a stupid question, but is there a way in IDL to tell
>>> whether a string contains a valid representation of a particular data
>>> type? For example an integer? I can do something likeOne "unixy" way to do this is with regular expressions. I end up using
> these all the time (any should more).
>
> Play with something like:
> IDL> a='hello'
> IDL> print, stregex(a, '[a-zA-Z]', /boolean)
> 1
> IDL> b='3.14159'
> IDL> print, stregex(b, '[a-zA-Z]', /boolean)
> 0
> And one could work out a better regexp that looked for other
> non-numeric chars as well.
>
> This is good and easy.
>
> Cheers,
> Brian
|
|
|