Re: string variables

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Wed, 20 Sep 2006 12:26:17 -0600 (MDT)

Hello

The original question was:
>_______________________________________________________
>
>Is there a way to get substrings of a string variable?
>_______________________________________________________

Another user sent me a followup comment:

>_______________________________________________________
>I think the question may have been about searching
>for a substring. Or at least that's a question I've had.
>As of now I've just used do loops, but I wonder if there
>is some automated way to do this.
>_______________________________________________________

I've attached two functions and a simple test.

function isStrSubset(S[1]:string, s[1]:string)
; return True or False is "s" is a subset of "S"

function indStrSubset(S[1]:string, s[1]:string)
; return the indices of the characters
; of "S" of which "s" is a subset.

Maybe, these should be included in the a035 release
of contributed.ncl ?

D

>
>> Is there a way to get substrings of a string variable?
>>> _______________________________________________________
>>
>>
>> ncl> s = "apple_orange_banana"
>> ncl> sc= stringtochar(s)
>> ncl> print( sc )
>>
>> Variable: cs
>> Type: character
>> Total Size: 20 bytes
>> 20 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [20]
>> Coordinates:
>> (0) a
>> (1) p
>> (2) p
>> (3) l
>> (4) e
>> (5) _
>> (6) o
>> (7) r
>> (8) a
>> (9) n
>> (10) g
>> (11) e
>> (12) _
>> (13) b
>> (14) a
>> (15) n
>> (16) a
>> (17) n
>> (18) a
>> (19) 0x00
>>
>>
>> ncl> s1 = chartostring(cs(0:4))
>> ncl> print(s1)
>>
>> Variable: s1
>> Type: string
>> Total Size: 4 bytes
>> 1 values
>> Number of Dimensions: 1
>> Dimensions and sizes: [1]
>> Coordinates:
>> (0) apple
>>
>> ncl > s2 = chartostring(cs( 6:11) ) ; orange
>> ncl > s3 = chartostring(cs(13:18) ) ; banana
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk_at_ucar.edu
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>>

function isStrSubset(S[1]:string, s[1]:string)
; return True or False is "s" is a subset of "S"
local SC, sc, nsc, nSC, n, sTF
begin

   SC = stringtochar(S) ; main
   sc = stringtochar(s) ; subset

   nSC = dimsizes( SC ) - 2 ; extra 'end of char' at end
   nsc = dimsizes( sc ) - 2

   sTF = False
   if (nsc.le.nSC) then ; nsc must be <= nSC
       do n=0,nSC-nsc
          if (all(SC(n:n+nsc).eq.sc(0:nsc)) ) then
              sTF = True
              return (sTF)
          end if
       end do
   end if
   
   return (sTF)
end

function indStrSubset(S[1]:string, s[1]:string)
; return the indices of the characters
; of "S" of which "s" is a subset.
local SC, sc, nsc, nSC, n, ii
begin

   SC = stringtochar(S) ; main
   sc = stringtochar(s) ; subset

   nSC = dimsizes( SC ) - 2 ; extra 'end of char' at end
   nsc = dimsizes( sc ) - 2

   if (nsc.le.nSC) then ; nsc must be <= nSC
       do n=0,nSC-nsc
          if (all(SC(n:n+nsc).eq.sc(0:nsc)) ) then
              ii = ispan(n,n+nsc,1)
              return( ii )
          end if
       end do
   end if

   ii = new ( 1, "integer", -999)
   return (ii)
end

begin
   X = "apple_orange_banana"
   x = "orange"
   print ("====================================")

   xTF = isStrSubset(X, x)
   if (xTF) then
       print(x+" is a subset of input string")
   else
       print(x+" is NOT a subset of input string")
   end if

   j = indStrSubset(X, x)
   print(j)
   if (.not.all(ismissing(j))) then
       XC = stringtochar( X )
       print( XC(j) )
   end if

   print ("====================================")

   y = "strawberry"
   yTF = isStrSubset(X, y)

   if (yTF) then
       print(y+" is a subset of inout string")
   else
       print(y+" is NOT a subset of inout string")
   end if

   k = indStrSubset(X, y)
   print(k)
end

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Sep 20 2006 - 12:26:17 MDT

This archive was generated by hypermail 2.2.0 : Mon Sep 25 2006 - 11:45:07 MDT