Re: integers from a string

From: Dennis Shea (shea AT cgd.ucar.edu)
Date: Wed Sep 21 2005 - 17:56:01 MDT


A minor expansion of what Adam sent. The following
creates an integer time of the form yyyymmddhh

   stime = "01/01/1997 (18:00)" ; MM/DD/YYYY (HH:NN) {ignore NN}
   tmp_c = stringtochar(stime)
   itime = stringtointeger((/tmp_c(6:9)/)) * 1000000 \
         + stringtointeger((/tmp_c(0:1)/)) * 10000 \
         + stringtointeger((/tmp_c(3:4)/)) * 100 \
         + stringtointeger((/tmp_c(12:13)/))
   
yields
   itime = 1997010118
   
The point is that groups of characters [eg: characters 6 thru 9]
can be combined using NCL's array syntax.

Good luck
D

>I would suggest using the stringtochar function, which places each
>individual letter/number/etc into an array. Then grab the specific
>character (=integer) you want, and pass it back thru chartostring and
>stringtointeger:
>
>ts = "Testing 1-2"
>a = stringtochar(ts)
>print(a)
>val1 = stringtointeger(chartostring(a(8)))
>val2 = stringtointeger(chartostring(a(10)))
>
>This will work assuming that the locations of the integers doesn't
>change from string to string if you are reading multiple strings.
>
>Adam
>
>Colleen Henry wrote:
>> I have a string that contains two integers, and ultimately I want to be
>> able to read them into separate variables or into a two-dimensional
>> array. I've tried the "stringtointeger" function, but that only captures
>> the first value. Are there any other options besides this function?
>>
>>
>> ********************
>> Colleen Henry
>> Graduate Student
>> Department of Earth and Atmospheric Sciences
>> 550 Stadium Mall Dr.
>> Purdue University
>> West Lafayette, IN 47906
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk AT ucar.edu
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>--
>--------------------------------------------------------------
>Adam Phillips asphilli AT ucar.edu
>National Center for Atmospheric Research tel: (303) 497-1726
>ESSL/CGD/CAS fax: (303) 497-1333
>P.O. Box 3000
>Boulder, CO 80307-3000 http://www.cgd.ucar.edu/cas/asphilli
>_______________________________________________
>ncl-talk mailing list
>ncl-talk AT ucar.edu
>http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
ncl-talk AT ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk



This archive was generated by hypermail 2b29 : Fri Sep 23 2005 - 14:29:36 MDT