| 
    
     | 
       
        | Author |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          I have run into a couple situations when working with a flat file where it would be nice to have a string function of PAD.
           | Posted: March 02 2013 at 10:42 | IP Logged |   |  
           | 
 |  
 It would be something like:
 PAD(string,L/R,fill_char,result_len)
 
 usage:
 PAD("hello","L","9",10) resulting in: "99999hello"
 PAD("hello","R",".",10) resulting in: "hello....."
 
 
 |  
        | Back to Top |     |  
        |  |  
        | dhoward Admin Group
 
  
  
 Joined: June 29 2001
 Location: United States
 Online Status: Offline
 Posts: 4447
 | 
          gg102,
           | Posted: March 02 2013 at 13:14 | IP Logged |   |  
           | 
 |  
 Take a look at the "fill" function and let me know if it will work for you.  Your examples would be coded like this:
 
 
 
| Code: 
 
    
    | 
      
       | fill("9",5) + "hello"
 "hello" + fill(".",5)
 
 |  |  |  
 Dave.
 
 |  
        | Back to Top |       |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          Thank you Dave for your input and quick response.
           | Posted: March 02 2013 at 18:00 | IP Logged |   |  
           | 
 |  
 The "fill" function is not exactly the same.
 
 Using "fill" you pad the string by an amount, not specify the length of the resultant string.
 To do this, you have to know the source length of the string up front (at codetime not runtime).
 
 For myself, I wrote a macro which uses the fill function.
 
 basically, using the fill function:
 
 if (error check) then exit
 else
 IF (RIGHT)
 "string" + fill( "pad_char",(result_len - LEN("string") )
 IF (LEFT)
 fill( "pad_char",(result_len - LEN("string")) + "string"
 
 This allows you to fill to a result_length, at runtime.
 
 Yes, you can "get there from here" using "fill", but the PAD function does the work for ya.
 
 Typically, the stringlen of data in a variable would not be known at codetime.
 
 it's just easier to call:
 
 PAD("string","R",".",LEN)
 
 I use this in flat file use where I want to write a column of data:
 
 date       time &nbs p;  description    value
 ---------------------------------------
 01/20/2013 00:00   room  one         &nb sp; 3
 01/20/2013  00:00   light     &n bsp;        5
 01/20/2013  00:00   fan     &nbs p;          11
 01/20/2013 00:00   another function   6
 01/20/2013  00:00   meter     &n bsp;       22
 01/20/2013 00:00   front meter        3
 
 
 the stringlength of the variable data would be unknown until runtime. So to write
 this in columns would be difficult without a pad function.
 PAD-R for the description and PAD-L for the value
 
 Thus the file write detail loop would be:
 ph_writefile("{LOG_FILE_NAME}",0,
 today() + "   " +
 PAD("{variable1}","R"," ",20) +
 PAD("{variable2}","L"," ", 3) +
 char(10) + char(13)
 )
 
 
 Anyways, no action required, just a suggestion.
 
 PH ROCKS!   [:P]
 |  
        | Back to Top |     |  
        |  |  
        | gg102 Senior Member
 
  
 
 Joined: January 29 2013
 Location: United States
 Online Status: Offline
 Posts: 246
 | 
          Well, the WEB page didn't make the columns correct but you get the idea.......
           | Posted: March 02 2013 at 18:01 | IP Logged |   |  
           | 
 |  
 |  
        | Back to Top |     |  
        |  |  |  |