Radinator
Radinator

Reputation: 1088

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!

Upvotes: 1

Views: 1849

Answers (2)

jmarkmurphy
jmarkmurphy

Reputation: 11493

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;

Upvotes: 2

Barry
Barry

Reputation: 448

This trick seems to work for me

charseq = %Str(%Addr(chrseq));

But there are a couple of other options to this. First, you could use a pointer and the based keyword over charseq

Dcl-S charptr Pointer Inz(%Addr(charseq));
Dcl-S charseq Char(100);
Dcl-S chrseq  Char(1) Dim(100) Based(charptr);

Or you could remove the pointer and based keyword to use a data-structure and the pos keyword instead

Dcl-Ds memoryDS;
  charseq Char(100);
  chrseq Char(1) Dim(100) Pos(1);
End-ds;

Then, anytime you assign to a chrseq index it will be over charseq memory

//Any changes to chrseq will happen in charseq memory
chrseq(1) = 'H';
chrseq(2) = 'e';
chrseq(3) = 'l';
chrseq(4) = 'l';
chrseq(5) = 'o';

Dsply %Subst(charseq:1:52);

Upvotes: 2

Related Questions