Re: type conversion from string to logical

From: Saji N Hameed <saji_at_nyahnyahspammersnyahnyah>
Date: Sun Dec 06 2009 - 22:26:27 MST

Hi Jonathan,

There is no built-in function to do string_to_logical. Here is a
hand-made one..

function string_to_logical(args:string)
begin
  cargs=str_lower(args)
  nstrs=dimsizes(args)
  largs=new(nstrs,logical)

  do i = 0,nstrs-1
    if any( (/"true","false"/) .eq. cargs(i))
      largs(i)=cargs(i) .eq."true"
    else
      largs(i) = False
    end if
  end do
  return(largs)
end

cheers!
saji

* Jonathan Vigh <vigh@atmos.colostate.edu> [2009-12-07 04:53:27 +0000]:

> Greetings NCL'ers,
> Maybe this is a completely silly question, but is there an NCL
> built-in to convert a variable of type string to a variable of type
> logical? I suppose it would be trivial to write such a function, but I
> couldn't seem to find one. Simply assigning a logical to the value of a
> string ("True" or "False") doesn't work (see below). According to the
> documentation
> (http://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclDataTypes.shtml#Coercion),
> it doesn't seem that string types are coercible to anything else.
>
> Jonathan
>
> Copyright (C) 1995-2009 - All Rights Reserved
> University Corporation for Atmospheric Research
> NCAR Command Language Version 5.1.1
> The use of this software is governed by a License Agreement.
> See http://www.ncl.ucar.edu/ for more details.
> ncl 0> stringvar = "False"
> ncl 1> logvar = new(1,logical)
> ncl 2> logvar = stringvar
> fatal:Assignment type mismatch, right hand side can't be coerced to type
> of left hand side
> fatal:Execute: Error occurred at or near line 2
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>

-- 
Saji N. Hameed
APEC Climate Center          				
1463 U-dong, Haeundae-gu,                               +82 51 745 3951
BUSAN 612-020, KOREA                    		saji@apcc21.net
Fax: +82-51-745-3999
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Sun Dec 6 22:25:03 2009

This archive was generated by hypermail 2.1.8 : Mon Dec 07 2009 - 16:12:30 MST