Re: char/string

From: Dave Allured <dave.allured_at_nyahnyahspammersnyahnyah>
Date: Mon, 10 Nov 2008 14:59:15 -0700

Oliver,

Here is something that may be close to what you want. Add the
following function to your includes. This assumes that you would be
using this function repeatedly to make the overall main program more
readable:

;----------------------------------------------------------------
;
; char.ncl -- Convert NCL string directly to a single character.
;
; Returns the first character of a string as type character.
; Any characters after the first are ignored.
; If an empty string is input, output is ascii 0 = null char.
;
;----------------------------------------------------------------

function char (str[1]:string)
local c

begin
    c = stringtochar (str) ; convert to char array
    return (c(0)) ; return first char as char
end

Then your test case would become fairly succinct:

    a="TOT_PREC_HYBY_acc*"
    i=ind(stringtochar(a) .eq. char("*"))

Dave Allured
CU/CIRES Climate Diagnostics Center (CDC)
http://cires.colorado.edu/science/centers/cdc/
NOAA/ESRL/PSD, Climate Analysis Branch (CAB)
http://www.cdc.noaa.gov/

Oliver.Fuhrer_at_meteoswiss.ch wrote:
> Dear all,
>
> I would like to find the occurences of "*" in a string. I thought an
> elegant way of doing this is...
>
> a="TOT_PREC_HYBY_acc*"
> i=ind(stringtochar(a) .eq. "*")
>
> Now, it seems that NCL coerces the stringtochar(a) back to a string, as
> it supposes that "*" is a string an not a character. The way it would
> work is...
>
> a="TOT_PREC_*_HYBY_acc*"
> b=stringtochar("*")
> i=ind(stringtochar(a) .eq. b(0))
>
> I was just wondering, if there is a way of telling NCL that a explicitly
> specified variable such as "*" should be a character, and not a string.
>
> Thanks,
> Oli
>
>
> ________________________________________
>
> Oliver Fuhrer
> Numerical Models
>
> Federal Departement of Home Affairs FDHA
> Federal Office of Meteorology and Climatology MeteoSwiss
>
> Kraehbuehlstrasse 58, P.O. Box 514, CH-8044 Zurich, Switzerland
>
> Tel. +41 44 256 93 59
> Fax +41 44 256 92 78
> oliver.fuhrer_at_meteoswiss.ch
> www.meteoswiss.ch - First-hand information
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Nov 10 2008 - 14:59:15 MST

This archive was generated by hypermail 2.2.0 : Fri Nov 14 2008 - 16:42:12 MST