Re: type conversion from string to logical

From: Jonathan Vigh <vigh_at_nyahnyahspammersnyahnyah>
Date: Mon Dec 07 2009 - 13:49:20 MST

Thanks Saji!

I might modify this a little bit so as to map missing strings to missing
logical. Right now it seems that any string that isn't "True" gets set
to logical False - maybe this is fine. The reason this issue came up is
that I'm reading in an ASCII text file by column (so by character ranges
and then converting the values to the respective variable types) and was
surprised when True and False's couldn't be read into a logical array.

Jonathan

Saji N Hameed wrote:
> 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
>>
>>
>
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Dec 7 13:49:30 2009

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