Eliminating Double Spaces in alpha

I have an (largely) formatting issue that I would like to solve in a neat way if possible.

When using a natural date format, I sometimes want to display it natively as a ‘nice’ date.

I can use the edit-mask properties to do some manipulation on the date format to get it to specific string representations. That is great.

Recently I was asked to present the date in the format ‘Weekday Month DD, YYYY’.

I can do this with:

 define data local 
01 #date-alpha (a8)
01 #date (d)
01 #date-print (a30)  
end-define

#date-alpha := *datn
move edited #date-alpha to #date(em=yyyymmdd)
move edited #date(EM=N(9)' 'L(10)^DD','^YYYY) to #date-print
print #date-print
end

However, when the date is early in the month, the ^DD result appears as ‘08’ which is not pretty.

So I’ve replaced it with

move edited #date(EM=N(9)' 'L(10)^ZD','^YYYY) to #date-print

which now results in the 0 being replaced with a blank character.

Now I’m left with the task of eliminating this character from the string.

examine #date-print for '  ' replace ' '

was my first attempt, however, the double space is simply recognized as a blank string array. Similarly, H’2020’ also resulted in no observable change.
I could create a more complex set of statements to solve this for this specific case:

define data local 
01 #date-alpha (a8)
01 #date (d)
01 #date-print (a30)  
01 #pos (i4)
01 #len (i4)
01 #i (i4)
01 #is-space (L)
end-define

#date-alpha := *datn
move edited #date-alpha to #date(em=yyyymmdd)
move edited #date(EM=N(9)' 'L(10)^ZD','^YYYY) to #date-print
examine #date-print for ' ' giving length #len
for #i = 1 to #len
  #pos := #len - #i + 1 
  examine substring(#date-print, #i, #pos) for ' ' giving position #pos
  if #pos = 1
    if #is-space = true
      #pos := #len - #i + 1
      examine substring(#date-print, #i, #pos) for ' ' delete
      examine #date-print for ',2' replace ', 2' /* fix for the year space as it gets caught in the remaining string
    else
      #is-space := true
    end-if
  else
    #is-space := false
  end-if
end-for
print #date-print

end

I feel like there should be a universal, simple method to eliminate a double space in a string within Natural. Would anyone care to elaborate? Maybe a simpler version of the per-character for loop which spits each element into a new string only if the previous one wasn’t a space?

Thanks!

What you were using to eliminate the double space is close, just need to use the FULL option:
EXAMINE #DATE-PRINT for FULL ’ ’ replace ’ ’

An additional statement EXAMINE … REPLACE … is not necessary, when you use an appropriate EM depending on whether there is a leading 0 in the day number.

DEFINE DATA LOCAL                                                  
01 #DATE-ALPHA                       (A8)                                   
01 REDEFINE #DATE-ALPHA                                            
  02 #YYYYMM                            (A6)                                   
  02 #D1                                     (N1)    /* is the value 0 ?                               
  02 #D2                                     (A1)                                   
01 #DATE                                  (D)                                    
01 #DATE-PRINT                      (A30)                                  
END-DEFINE                                                         
*                                                                  
#DATE-ALPHA := '20190509'                                          
MOVE EDITED #DATE-ALPHA TO #DATE(EM=YYYYMMDD)                      
*                                                                  
IF #D1 NE 0                                                        
  MOVE EDITED #DATE(EM=N(9)' 'L(10)^DD','^YYYY) TO #DATE-PRINT     
ELSE                                                               
  MOVE EDITED #DATE(EM=N(9)' 'L(10)ZD','^YYYY)  TO #DATE-PRINT     
END-IF                                                             
PRINT #DATE-PRINT                                                  
END