how to add a number of blank-spaces in free-format RPG

625 pts.
AS400 ile
My requirement is: I need to concatenate different fields having different values into the flat-string(blank initially), separated by a number of blanks(spaces), which may vary from field to field. for e.g. MyStr = 'FIRST_NAME LAST_NAME 20060101 STREET1 CITY11 STATE111 ... ' and so on where MyStr = Flat String, FIRST_NAME, LAST_NAME, 20060101 etc., are my field-values which i need to concatenate to MyStr. please note that FIRST_NAME & LAST_NAME are seperated exactly by 10-spaces and LAST_NAME & 20060101 are seperated exactly by 5-spaces, STREET1 & CITY11 are seperated exactly by 3-spaces etc., which will vary fld to fld. Iam using free-format RPG & if I use a work var to loop through & concatenate each field as below WrkStr = ' '; Loop; WrkStr = WrkStr + Fname; End-Loop; MyStr = WrkStr; and when i finally move this WrkStr into Mystr as above, to my surprise i don't see anything in MyStr(i.e. it's blank). Iam not getting what to do. Please help. Thanks in Advance

Software/Hardware used:
AS/400, OS/400, RPGILE

Answer Wiki

Thanks. We'll let you know when a new response is added.

Sorry Guys, Here above, Iam unable to show the number of spaces the fields are separated by, as it is taking only 1 space, when i save, no matter how many spaces i give.
pls. assume that the fields are seperated by those no. of spaces as mentioned in the question. …Thanks

Discuss This Question: 10  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Littlepd
    Look into the %trim, %triml & %trimr Built-In Functions.
    WrkStr = ' ';
    WrkStr = %trimr(WrkStr) + Fname;
    MyStr = WrkStr;
    1,130 pointsBadges:
  • TomLiotta
    ...when i finally move this WrkStr into Mystr as above, to my surprise i don't see anything in MyStr(i.e. it's blank). We need to see the definitions of WrkStr, Mystr and Fname in order to guess. To preserve spaces, surround the RPG instructions with the "code" and "/code" tags from the selections above the entry box. Tom
    125,585 pointsBadges:
  • TomLiotta
    As in your previous question, I can't see why you don't create a data structure that positions your fields exactly the way you want them. However, Littlepd probably provided the technical answer to this problem. The reason is that WrkStr has some length attribute, we'll say 100 bytes. It starts with a value of all blanks. You then execute this statement:
    WrkStr = WrkStr + Fname;
    That statement takes the 100 blanks from WrkStr and concatenates the characters from Fname to it. If Fname is 20 bytes long, the resulting value is 120 bytes long (100 + 20). Then those 120 bytes are assigned to WrkStr again. But since WrkStr is only 100 bytes long, only the first 100 bytes can fit in WrkStr. And the first 100 bytes are all blanks. By using %TRIM() or %TRIMR(), the previous blanks are trimmed before Fname is concatenated. That allows Fname to be put into the first bytes of WrkStr. Note that this trimming will happen every time through the loop. The trailing blanks will be removed each time. If you need an exact number of blanks between each field and you insist upon concatenating, you will need to include the exact number of blanks each time. For example:
    WrkStr = %trimr(WrkStr) + '          ' + Fname;
    or perhaps:
    D myBlanks        s             50A   inz( ' ' )
      WrkStr = %trimr(WrkStr) + %substr( myBlanks:1:10 ) + Fname;
    You can use a variable to choose a substring length if different lengths are needed between fields. Note that you can do this in a loop, but you'll probably need to set WrkStr to the value of your first sub-field before the loop. This question has become different from your previous one. In this one, the sub-fields are separated by an exact number of spaces and different sub-fields are separated by different numbers of spaces, which makes very little sense to me. Do you need each sub-field trimmed and then a fixed number of spaces after it? Tom
    125,585 pointsBadges:
  • svankipu
    Guys, I apologize for late reply. LittlePD, Thanks very much for the reply. Tom, Thanks a lot for the reply As u said ..."Do you need each sub-field trimmed and then a fixed number of spaces after it?" Yes Tom! You are exactly right. I need it this way only. pls. let me know.
    625 pointsBadges:
  • TomLiotta
    MyStr = 'FIRST_NAME LAST_NAME 20060101 STREET1 CITY11 STATE111 ... '
    To get an exact number of spaces between each field, there are two general alternatives. You can replace the above statement with something like this:
    MyStr = %trim(FIRST_NAME) + '          ' +
             %trim(LAST_NAME) + '     ' +
             %trim('20060101') + '     ' +
             %trim(STREET1) + '   ' +
             %trim(CITY11) + '     ' +
             %trim(STATE111) ... '
    Or you could be a little more documenting and go with something like:
    D myBlanks        s             50A   inz( ‘ ‘ )
     MyStr = %trim(FIRST_NAME) + %substr( myBlanks:1:10 ) +
             %trim(LAST_NAME) + %substr( myBlanks:1:5 ) +
             %trim('20060101') + %substr( myBlanks:1:5 ) +
             %trim(STREET1) + %substr( myBlanks:1:3 ) +
             %trim(CITY11) + %substr( myBlanks:1:5 ) +
             %trim(STATE111) ... '
    That kind of makes it easier to see exactly how many spaces are needed between each sub-field. You would define myBlanks to be at least as long as the longest set of blanks that you needed. I chose 50 bytes because I didn't know what the maximum should be. The longest that you mentioned was only 10 bytes, so that might be enough. But be aware that this will not make it easy for another program to access the sub-fields in any kind of data structure. It will make it more difficult for another program to handle the data. So far, it's not making sense to do this. Tom
    125,585 pointsBadges:
  • NathanHoffman

    D blanks             s                     10A  inz('          ')

    num = %len(fieldname) - %Len(%trim(Fieldname);
    fieldname = %Subst(blanks:1:num) + fieldname;
    20 pointsBadges:
  • azohawk
    I would tend to use "myblanks as suggested by TomLiotta, but another option would be to create blank fields of the various lengths that you need
    D MYBLANK10           S         10A   INZ(*blank)
    D MYBLANk3            S          3A   INZ(*blank)
    MyStr = First_Name + MyBlank10 + Last_Name + MyBlank5 +....
    For those who have to look at this after you, this may (I said may not will) make more sense. Just another option. 
    4,055 pointsBadges:
  • NathanHoffman
    Just a note, My method will figure out automatically exactly how many blanks you need inserted in front of the word no matter how long or short the word is, each and every time. all you have to do is initialize the field with a value. it does so by taking the (field size - portion used) = unused amount of spaces. Then Adds unused spaces to the beginning of the initialized value making a full variable. unless the field's value will be the same length everytime, in that case, shorten your field
    20 pointsBadges:
  • kissau

    Use defined data structure:
    D dsLine          DS              
    D                               25A
    D   wk1                         10A
    D                                5A
    D   wk2                         10A
    D                                5A
    D   wk3                         10A

    10 pointsBadges:
  • TheRealRaven
    @kissau : It depends on the actual requirements. A DS like yours won't work if "FIRST_NAME & LAST_NAME are seperated exactly by 10-spaces" is an exact requirement. That is, if FIRST_NAME has one trailing blank, then the result would be 11 blanks between the two fields -- the one trailing blank plus the 10 blanks from the DS sub-field.

    If example inputs and outputs were given, it might be clear what's really needed.
    35,920 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: