Contact Us

Previous

Natural Tip of Week of May 12, 2008

Next

Subject: Natural string handling
Title:
Testing a string for a non-printable character

This is came up with one of Boston Univerity's programmers. Because I knew his exceptionable understanding of Natural, I was surprised to find out the approach that was used to address answering this question. The program scenario was for one field only. Let's use the following definition:

01 #ALPHA-FIELD (A40)

Natural provides the ability to mask characters and test for attributes. MASK(P) is good for asking if a character is printable. For Natural, by definition that the MASK(P) is true if the character is (U)pper case, (L)ower case, (N)umeric digit or (S)pecial character. More on the MASK (S) later.

The technique used by my colleague was the following:

01 #ALPHA-FIELD (A40)
01 REDEFINE #ALPHA-FIELD
  02 #ALPHA-CHAR (A1/1:40)
01 #LEN (I2) CONST <40>
...
FOR #I = 1 TO #LEN
  IF #ALPHA-CHAR NE MASK(P)
    ... do something
    ESCAPE BOTTOM
  END-IF
END-FOR

The first surprise was to see that a FOR loop was used. This is totally unnecessary; one should use array analysis vs looping through one character at a time. In other words, the proper coding technique is to eplace the FOR loop & IF statement with this IF statement:

IF #ALPHA-CHAR (*) NE MASK(P)
   ... do something
END-IF

This is equivalent to Natural asking:

IF #ALPHA-CHAR (1) NE MASK(P) AND
   #ALPHA-CHAR (2) NE MASK(P) AND
   #ALPHA-CHAR (3) NE MASK(P) AND
etc.
END-IF

However, this article is not quite done. I was surprised to discover that the character '!' (exclamation mark) did not qualify as a printable character. This had to mean that it was not considered a special character. Indeed, here is a program to test all 256 hex values from X'00' to X'FF'.

DEFINE DATA LOCAL             
1 #BIN-CHAR (B1)              
1 REDEFINE #BIN-CHAR          
  2 #ALPHA-CHAR (A1)          
1 #X (I2)                     
1 PRINTABLE (L)               
1 SPECIAL   (L)                 
END-DEFINE                    
*                             
FOR #X = 0 TO 255             
  MOVE #X TO #BIN-CHAR        
  IF #ALPHA-CHAR = MASK(P)    
    MOVE TRUE TO PRINTABLE    
  ELSE                        
    MOVE FALSE TO PRINTABLE   
  END-IF                      
  IF #ALPHA-CHAR = MASK(S)    
    MOVE TRUE TO SPECIAL      
  ELSE    
  END-IF                                         
  DISPLAY #X 'Char' #ALPHA-CHAR 'Bin' #BIN-CHAR  
    5X PRINTABLE (EM=NO/YES) SPECIAL (EM=NO/YES) 
END-FOR                                          
*                                                
END      
                                                             

After investigation I realized that Natural did not consider the exclamation mark a special and therefore a printable character. I asked myself "Why?"

Once again a Sag-l expert - Helmut Spichtinger - explained that the macro named NTSCTAB defined in the NATCONFG module is responsible defining what is special and what is not. I have an issue that Natural differs from what I consider printable for several characters. To change this one would either re-build the NTSCTAB table or dynamically override with the SCTAB parameter.

 

Here are three sponsors who have more materials on Natural & Adabas tips and techniques:

ZZUtils   WH&O   Storr Consulting