Re: Converting strings to numbers

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Wed, 29 Nov 2006 21:21:12 -0700 (MST)

> I need to convert strings to integers or reals. Type converters like
> stringtoint() will do this. However, I need better error control than that
> function provides, because I am trying to validate unknown data.
> For example, "12x3" would be misinterpreted as the number 12 because
> stringtoint() apparently stops parsing when it reaches a non-digit, then
> merrily outputs a number with no problem indication.
> Is there any way in NCL, short of parsing every character myself, to check
> whether a complete string is a valid number? I need a true/false result so
> that my program can take special action, not just an error message from NCL.
> The query functions like isnumeric() check variable type, not string contents.
> Thank you for any advice.
> _______________________________________________

I don't think that without parsing there is a way.
fortran, c, idl, ... would require that the
user specift the format.

I am sure this is totally wrong ... but if the strings are
on a file, say "z.asc"

/home/shea> ncl
 Copyright (C) 1995-2006 - All Rights Reserved
 University Corporation for Atmospheric Research
 NCAR Command Language Version 4.2.0.a034
 The use of this software is governed by a License Agreement.
 See for more details.
ncl 0> s = asciiread("z.asc", -1, "integer")
ncl 1> print(s)

Variable: s
Type: integer
Total Size: 8 bytes
            2 values
Number of Dimensions: 1
Dimensions and sizes: [2]
(0) 12
(1) 3

ncl 2> dims = dimsizes(s)
ncl 2> print(dims)

Variable: dims
Type: integer
Total Size: 4 bytes
            1 values
Number of Dimensions: 1
Dimensions and sizes: [1]
(0) 2
ncl-talk mailing list
Received on Wed Nov 29 2006 - 21:21:12 MST

This archive was generated by hypermail 2.2.0 : Thu Nov 30 2006 - 14:52:19 MST