comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Testing validity of numerical content of a string
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Testing validity of numerical content of a string [message #51742] Wed, 06 December 2006 17:49 Go to next message
Brian Larsen is currently offline  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 #51743 is a reply to message #51742] Wed, 06 December 2006 17:38 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
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
>
> test = fix(str)
>
> which will return zero (and throw an error message) if str is, say,
> "hello". But is there a way to tell whether type conversion of a string
> will work if attempted?
>
> Anybody have something clever at hand?

Clever's not really my thing. You would have to go to JD
for that. But FSC_FIELD has a neat little function named
"VALIDATE" that validates whether a string is a float or
double or an integer, whatever it is suppose to be. You
might be able to steal that and do something clever with
it. :-)

http://www.dfanning.com/programs/fsc_field.pro

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Testing validity of numerical content of a string [message #51841 is a reply to message #51742] Wed, 06 December 2006 18:50 Go to previous message
Brian Larsen is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Adjacent plot question
Next Topic: Logarithmic Binning with Histogram function?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 14:01:14 PDT 2025

Total time taken to generate the page: 0.00670 seconds