Re: testing for a blank (null) character

From: David Ian Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Tue, 5 Jun 2007 17:25:35 -0600

On Jun 5, 2007, at 5:11 PM, Jonathan Vigh wrote:

> Hmmmmm, I found a workaround (actually I think I've solved this before,
> but forgot my solution).
>
> Since I originally read each line into a string, I can use NCL's string
> function 'strlen' to check if each string has a nonzero length:
>
> if (strlen(tStr(i)) .gt. 0) then ; do some stuff to keep line
>
Yes this is probably the easiest way to do it.

> I'm still curious to know how one would test whether a character
> variable was null however. I don't think I was able to get a test using
> ismissing to work.
>

You can explicitly create a variable with the NULL character using:

nullChar = inttochar(0)

  -dave

> Jonathan
>
>
> On Tue, 2007-06-05 at 22:22 +0000, Jonathan Vigh wrote:
>> Greetings,
>> I'm reading in a mixed type irregular ascii text file. Some of the
>> lines are completely blank (no spaces). I'd like to test against this
>> and take out the blank lines. Is there any way to test to see if the
>> first character of a line is null?
>>
>> First I read my file as a unlimited dimensioned array of strings (each
>> line is one string).
>>
>> tStr = asciiread("foo.txt", -1,"string")
>>
>> Then I convert the 1D string array into a 2D character array:
>>
>> tChr = stringtochar(tStr)
>>
>> Next, the first character of the i-th row in the character array is
>> read
>> into a character variable called FIRST:
>>
>> FIRST = tChr(i,0)
>>
>> Now I test to see if it's an exclamation point (a comment line):
>> if (FIRST .ne. "!") then . . .
>>
>> This test works, but if I test to see if the first character is
>> entirely
>> blank, the test fails and the script goes merrily on:
>>
>> if (FIRST .ne. " ") then
>>
>> This is because the FIRST is apparently equal to the null character
>> '/0'
>> and not a space. Is there a way to test if a character variable is
>> null?
>>
>> I'm guessing that there is something that can be done using the
>> FillValue or missing value attributes, but I guess I'm at a loss as
>> how
>> to properly do this.
>>
>> I've attached an example script and example input file if anyone wants
>> to look at this.
>>
>> Thanks,
>> Jonathan
>>
>> _______________________________________________
>> 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

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Jun 05 2007 - 17:25:35 MDT

This archive was generated by hypermail 2.2.0 : Thu Jun 14 2007 - 09:39:54 MDT