testing for a blank (null) character

From: Jonathan Vigh <vigh_at_nyahnyahspammersnyahnyah>
Date: Tue, 05 Jun 2007 22:22:46 +0000

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

;********************************************************
; char_test.ncl
;
; The purpose of this script is to read an irregular ascii
; text file and remove blank lines.
;********************************************************
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"

begin

;********************************
; get data
;********************************
  tStr = asciiread("input.txt", -1,"string") ; Info includes type so cases can be weeded out
  tChr = stringtochar(tStr)
  nrows = dimsizes(tStr)
  
  print(tStr)
    
  newtStr = new(nrows,"string")
    
; Read in the entire file, put all the lines which actually contain station data into a new array of strings
  k = 0
  do i = 0, nrows-1
   
     FIRST = tChr(i,0) ; next read the first letter in as a string
     
     if (FIRST .ne. "!") then ; only read the line if it's not a comment
        if (FIRST .ne. " ") then ; only read the line if it's not blank
           newtStr(k) = tStr(i) ; read a valid line into the new string array
           k = k + 1
        end if
     end if
     
  end do

  print(newtStr(0:k-1))


end

! Comment line
!23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
!

REAL LINE WITH DATA 13 56 70

ANOTHER REAL LINE 1 34

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Tue Jun 05 2007 - 16:22:46 MDT

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