Home The Company Publications Products Links Tips Jobs

How to Eliminate Blanks Between Words in a String?

EXAMINE

By Dieter W. Storr

Last update: 25 July 2007

Question:

How can you remove any number of blanks between words in a string.

Example

Input
A  B    C    D  E
Output
A B C D E

Solution 1 (good) - sent by Mark Keating:

EXAMINE #A20 FOR FULL H'4040' REPLACE WITH H'40FF'
EXAMINE #A20 FOR FULL H'FF40' DELETE 
EXAMINE #A20 FOR H'FF' DELETE 

Solution 2 (better) - sent by Jim Wisdom:

SEPARATE #STRING INTO #WORDS (*) WITH DELIMITER ' '
COMPRESS #WORDS (*) INTO #STRING

Solution 3 (best) - sent by Helmut Spichtinger:

EXAMINE #STRING FOR FULL H'40'    WITH DELIMITER H'40' DELETE  
EXAMINE #STRING FOR FULL H'4040'  REPLACE H'40'         
Jim Wisdom wrote after he compared solution 2 and 3:
"Comparison complete. Helmut’s suggestion of the 2 EXAMINE statements beat the pants off my SEPARATE/COMPRESS idea which also requires extra storage. I know what I’m going to do from now on!"

See also: Do not EXAMINE arrays.

Source code and tests from Dieter Storr

0010 DEFINE DATA LOCAL                                                  
0020 1 #STRING (A50)                                                    
0030 1 #WORDS  (A1/1:50)                                                
0040 1 #I   (P10)                                                       
0050 END-DEFINE                                                         
0060 *                                                                  
0070 SETA. SETTIME                                                      
0080 FOR #I = 1 TO 30000                                                
0090   #STRING := 'A   B C        D   E      F   '   
0100   EXAMINE #STRING FOR FULL H'4040' REPLACE WITH H'40FF'              
0110   EXAMINE #STRING FOR FULL H'FF40' DELETE                            
0120   EXAMINE #STRING FOR H'FF' DELETE                                   
0130 END-FOR                                                              
0140 WRITE   'Solution 1:' #STRING *TIMD(SETA.)                           
0150 *                                                                    
0160 #STRING := 'A   B C        D   E      F   '                          
0170 SETB. SETTIME                                                        
0180 FOR #I = 1 TO 30000                                                  
0190   SEPARATE #STRING INTO #WORDS (*) WITH DELIMITER ' '                
0200   COMPRESS #WORDS (*) INTO #STRING                                   
0210 END-FOR                                                              
0220 WRITE   'Solution 2:' #STRING *TIMD(SETB.)                           
0230 *                                                                    
0240 SETC. SETTIME                                                        
0250 FOR #I = 1 TO 30000                                                  
0260   #STRING := 'A   B C        D   E      F   '                
0270   EXAMINE #STRING FOR FULL H'40'    WITH DELIMITER H'40' DELETE      
0280   EXAMINE #STRING FOR FULL H'4040'  REPLACE H'40'                    
0290 END-FOR                                        
0300 WRITE   'Solution 3:' #STRING *TIMD(SETC.)     
0310 END
Executed stowed program -- online:

IBM z/OS 1.04.00, IBM 2066-0X2, 160 mips -- NAT413, COM631
Page      1                                      07-07-20  15:45:53
Solution 1: A B C D E F                                  13        
Solution 2: A B C D E F                                  23        
Solution 3: A B C D E F                                  13     

Page      1                                      07-07-20  15:47:00
Solution 1: A B C D E F                                  14        
Solution 2: A B C D E F                                  24        
Solution 3: A B C D E F                                  14 

Page      1                                      07-07-20  15:48:50
Solution 1: A B C D E F                                  13        
Solution 2: A B C D E F                                  22        
Solution 3: A B C D E F                                  14   

Page      1                                      07-07-20  15:54:09
Solution 1: A B C D E F                                  13        
Solution 2: A B C D E F                                  23        
Solution 3: A B C D E F                                  18     

Page      1                                      07-07-24  15:03:44
Solution 1: A B C D E F                                 170        
Solution 2: A B C D E F                                 259        
Solution 3: A B C D E F                                 335

Page      1                                      07-07-24  15:08:11
Solution 1: A B C D E F                                  54        
Solution 2: A B C D E F                                  79        
Solution 3: A B C D E F                                  54                       
Executed stowed program -- batch
IBM z/OS 1.04.00, IBM 2066-0X2, 160 mips -- NAT413
Page      1                                      07-07-24  15:16:03
Solution 1: A B C D E F                                  95        
Solution 2: A B C D E F                                  82        
Solution 3: A B C D E F                                  33       

Page      1                                      07-07-24  15:27:31
Solution 1: A B C D E F                                  39        
Solution 2: A B C D E F                                 146        
Solution 3: A B C D E F                                  57  

Page      1                                      07-07-25  11:12:48
Solution 1: A B C D E F                                  12        
Solution 2: A B C D E F                                  21        
Solution 3: A B C D E F                                  15               
Additional info from Steve Robinson:
I ran my comparison on both the PC and the mainframe.
I did have a slightly different string, here it is:
1 #STRING (A30) INIT <'ABC    D    E    F    G    H'>
Here are the PC results (100,000 iterations)
    PAGE #   1                    DATE:    Jul 21, 2007
    PROGRAM: BLANKS01             LIBRARY: INSIDE

         DUMMY FOR LOOP TIME                  7         70
         SEPARATE                            78        768
         EXAMINE 3 TIME                      24        230
         EXAMINE 2 TIME                      20        197
and here are the mainframe results (also 100,000 iterations)
  MORE                                                                           
     PAGE #   1                    DATE:    07-07-21                            
     PROGRAM: BLANKS01             LIBRARY: XSTRO                               
                                                                                
          DUMMY FOR LOOP TIME                  0          7                     
          SEPARATE                            16        134                     
          EXAMINE 3 TIME                      10         87                     
          EXAMINE 2 TIME                      11         91                     
Each of the above is representative of several runs on each platform. The two EXAMINE comes out best on the PC, while the three EXAMINEs prevails on the mainframe.

Interestingly enough, on the mainframe the SEPARATE is "not that bad"; only half again the two EXAMINEs. By contrast, on the PC the SEPARATE is triple the EXAMINEs.

Also of note; for shops considering such a transition, the mainframe numbers (same number of iterations) are only half (or a little less) the PC numbers. And my desktop PC is still a 600 MhZ system; a far cry from the 3 Gig screamers that are available for practically nothing these days.

Top Page



Back to NATURAL Tips, Tricks, Techniques -- Overview