Search a character in a string RPGLE

220 pts.
Tags:
RPGLE
In RPGLE, is there any easier way to find a position of a number of character occur in a string? For Eg, text = 'AAAA|BBB|CC| |DDD| |FFF|0.12|CCC| ' I want to get the position of 7th delimiter('|‘) because I would like to replace the value of 0.12. My way is to loop whole string and count the number of delimiter(’|‘). I wondering is %scan or %check could help on this? Thanks.
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

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.
  • philpl1jb

    If you are not using Free you can do this in a single step

    C      '|'     SCAN   Text    MyArray

    Where MyArray is an array   3, 0  Dim( 20)

    the 7th Element of MyArray would have the position of the 7th '|'


    In free there is %scan but it doesn't load an array

    The optional second factor in %scan is where to start the scan

    so it might look something like this.

    pos(1) = 1;

    For x = 2 to 8;

       pos(x) = %scan(Text: pos(x - 1);

    EndFor;


    54,090 pointsBadges:
    report
  • yeepiyaya
    a = '20A|20A|20A|20A|20A|20A|20A|20A|20A
    before = 'AA|BBB|CC|   |DDD| |FFF|0.12|CCC|  '
    after = AA|BBB|CC|   |DDD| |FFF|0.99999|CCC|  '
    What if I want the above result which will replace 0.12 to 0.99999? I need to use %SUBST but I can't exactly count the position of the string, some more there is blanks value. What I can confirm that is after 7th delimiter. I do have a way to do it but look complicated. So i need opinion from your guys. TQVM
220 pointsBadges:
report
  • philpl1jb

    So you need positions 7 and 8 .. but in my array they will actually be 8 and 9.


    pos(1) = 1;

    For x = 2 to 9;

       pos(x) = %scan(Text: pos(x - 1);

    EndFor;

    NewString = %SUBST( Text : 1 : pos(8) ) +

                          newvalue +

                         %SUBST( Text : pos(9) )


    54,090 pointsBadges:
    report
  • TheRealRaven
    You have odd requirements. For some reason, you want to replace a fixed value ('0.12') with another fixed value ('0.99999'), but only between certain occurrences of the delimiters (the 7th and 8th).

    Because that seems to be only a one-time process or a test question), it's probably best to do it manually or with STRSQL or with QSH. It doesn't seem like an actual programming problem. But you haven't told us anything about the circumstances nor what version you're running, and you haven't shown anything you've tried.

    A procedure can be written that does the work in any recent release. It would handle various circumstances, but it doesn't feel like it's very useful to do the work for a one-time process. If you told us how this is supposed to be used, it could be easier.
    36,025 pointsBadges:
    report
  • ToddN2000
    If this is a one time fix, use SQL of just DFU the record. If this is a true business problem, I'd follow Philpl1jb's suggestion and load it to an array. That may be the cleanest way to handle the problem. There may be other solutions, can you elaborate on the scope of the problem?
    134,720 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.

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

    Following

    Share this item with your network: