Examine statement

Hi,

I am looking to execute the following program:

[b]Define Data Local
01 #array (A5/1:10)
01 #index
end-define
**
move ‘12345’ to #array(1)
move ‘34567’ to #array(2)

examine #array(*) for " " giving index in #index
write #index
end[/b]

I always get a 0 as an answer. Why so? I can acheive this logic using a FOR loop syntax, but I want to stick to EXAMINE.
Please help me.

Hi Vikas,

In your code the #index variable is missing a data type, I assume its N2.

In the EXAMINE

you are looking for a blank value occurence in the array and getting 0 as index. I could say that FULL clause is missing on it.

If FULL is not specified, trailing blanks in the operand will be ignored. If FULL is specified for an operand, the entire value, including trailing blanks,will be processed.

Hence, change your statement to

examine full #array(*) for " " giving index in #index

and you should get a value as 3 in index where it finds first blank occurence.

Hope that helps!

Thanks a ton! It works.

Two points:

1 If you change the first MOVE to

move '1234' to #array(1)

then the value returned is 1. Be sure to fill each occurrence of #ARRAY; embedded blanks count.

2 If you are going to use the #INDEX as an index, then you should define it as I4. Otherwise you’ll incur overhead when Natural converts it to I4 internally.

Two points:

1 If you change the first MOVE to

move '1234' to #array(1)

then the value returned is 1. Be sure to fill each occurrence of #ARRAY; embedded blanks count.

2 If you are going to use the #INDEX as an index, then you should define it as I4. Otherwise you’ll incur overhead when Natural converts it to I4 internally.

Also, if you have embedded blanks in your array, it will return the first index with a blank.


DEFINE DATA LOCAL                                        
01 #ARRAY (A5/1:10)                                      
01 #INDEX (I4)                                           
END-DEFINE                                               
**                                                       
MOVE '12345' TO #ARRAY(1)                                
MOVE '34 67' TO #ARRAY(2)                                
EXAMINE FULL #ARRAY(*) FOR " " GIVING INDEX IN #INDEX    
WRITE #INDEX                                             
END                                                      

This returns 2.

If you want to find the first occurrence that is all blank, then you need

EXAMINE FULL #ARRAY(*) FOR FULL "     " GIVING INDEX IN #INDEX    

Jerome is correct. You must Examine FULL … for FULL …
.
When examining for multiple blanks, I like to define an empty variable of the same size as the array element and then Examine Full for the empty variable. This prevents errors in counting the correct number of blanks in a quoted literal.
.
Example:
DEFINE DATA LOCAL
1 #MAX (I4) CONST <10>
1 #5-BLANKS (A5) /* same Format/Length as #ARRAY element
1 #ARRAY (A5/1:#MAX) INIT
(1) <‘abcde’>
(2) <’ bcde’>
1 #COMMENT (A79)
1 #I (I4)
1 #INDEX (I4)
1 #NUMBER (I4)
END-DEFINE
*
FOR #I = 1 TO #MAX
DISPLAY #I #ARRAY(#I) /* just for reference
END-FOR
*
EXAMINE FULL #ARRAY(*) FOR FULL #5-BLANKS
GIVING NUMBER IN #NUMBER
GIVING INDEX IN #INDEX
COMPRESS ‘First Occurrence with 5 Blanks is #’ #INDEX INTO #COMMENT
PRINT #COMMENT
*
COMPRESS ‘Total Number of Blank Occurrences =’ #NUMBER INTO #COMMENT
PRINT #COMMENT
*
END