Search code examples
ibm-midrangerpgle

Moving array to charsequence using BIFs in (free)RPGLE


Can someone tell me how to convert a char array to a character?

I've defined

 D  charseq                     100a   value

and

 D  chrseq         s              1a   dim(100)

Now I reversed charseq and stored each character using BIF %subst(string:index:length) in the corresponding field in chrseq:

 C     1             do        101           x
 C                   eval      chrseq(101-x) = %subst(charseq:x:1)
 C                   enddo   

But now here is my problem: How do I convert the chrseq to my charseq? In good old RPG/400 I'd done this via MOVEA but is there any (modern) RPGLE version??

Here's the code:

 P*            -------------------------------------------------------------
 P*            getLastChar - Get last non-blank char in parameter
 P*            -------------------------------------------------------------
 P getLastChar     b                   export
 D getLastChar     pi             1a
 D*  Input-parameter
 D  charseq                     100a   value
 D*  Local fields
 D  lastChar       s              1a   inz(' ')
 D  chrseq         s              1a   dim(100)
 C
 C*            -------------------------------------------------------------
 C*            Error handling
 C*            -------------------------------------------------------------
 C     charseq       ifeq      *blanks
 C                   return    x'00'
 C                   endif
 C
 C*            -------------------------------------------------------------
 C*            Reverse input char sequence
 C*            -------------------------------------------------------------
 C     1             do        101           x
 C                   eval      chrseq(101-x) = %subst(charseq:x:1)
 C                   enddo
 C*  Now: How do I get chrseq(1) to chrseq(100) into charseq back?
 C*            -------------------------------------------------------------
 C*            Get last non-blank char
 C*            -------------------------------------------------------------
 C                   eval      charseq = %triml(charseq)
 C                   eval      lastChar = %subst(charseq:1:1)
 C
 C
 C                   return    lastChar
 C
 P getLastChar     e    

Thanks in advance!


Solution

  • So something similar to what Liam suggested is to use a data structure and just assign the data structure to your second parameter.

    dcl-proc ReverseCharacters;
      dcl-pi *n Char(100);
        charseq         Char(100) Const;
      end-pi;
    
      dcl-ds chards;
        chrseq         Char(1) Dim(100);
      end-ds;
    
      dcl-s charseq    Char(100);
      dcl-s x          Int(5);
    
      for x = 100 downto 1;
        chrseq(x) = %substr(charseq:x:1);
      end-for;
    
      return %triml(chrds);
    end-proc;
    

    But if all you want to do is return the last character of a string, I would do it this way.

    dcl-proc LastChar;
      dcl-pi *n Char(1);
        str     Varchar(100) Const;
      end-pi;
    
      if %len(str) = 0;
        return '';
      endif;
      return %substr(str: %len(%trimr(str)): 1);
    end-proc;