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

560 pts.
Tags:
AS400 ile
RPG
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: 5  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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Littlepd
    Look into the %trim, %triml & %trimr Built-In Functions.
    WrkStr = ' ';
    Loop;
    WrkStr = %trimr(WrkStr) + Fname;
    End-Loop;
    
    MyStr = WrkStr;
    1,130 pointsBadges:
    report
  • 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:
    report
  • 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:
    report
  • 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.
    560 pointsBadges:
    report
  • 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:
    report

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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

Following