« SAP::Rfc 1.36 for Active State Perl 5.8.x | Main | Ruby on Rails and ActiveRecord »

June 3, 2005

ABAP, Strings and Concatenation

Of late I have been heavily involved in writing ABAP code that seems to be coliding with UNICODE support related issues. For example - I have a fixed-width internal table, that I want to concatenate into a string, that is the requirement of a subsequent operation such as:
      UNENCODED = w_stringdata
      ENCODED   = base64data.
In this instance, you would expect to beable to just loop through a table, and concatenate the lengths of fixed working storage into a single string like so:
  loop at i_pdf into w_pdf.
    concatenate w_stringdata w_pdf into w_stringdata.
But this does not appear to be the case - try this:
  begin of rec,
  end of rec,
  w_row like rec,
  i_rows like standard table of rec,
  w_string type string.
  do 10 times.
    write sy-index to w_row right-justified.
    append w_row to i_rows.
  loop at i_rows into w_row.
    concatenate w_string w_row into w_string.

w_string will not be the expected 50 characters long, as it appears that the concatenate has removed a trailing space from each iteration of i_rows.

Basically - whats happening is that ABAP does not honour the true value of the fixed length character working storage. To me - fixed length is fixed length - if I wanted less than what was exactly in the piece of working storage, then I would expect to explicitly say so, not have something silent syphon it away. What is happening doesn't even conform to the generally accepted rules of C, where a string is NULL terminated, not the last non-whitespace value.

So - how do I get around this? So far, I have only found two, fairly rubbish solutions to this.
One is to pre-stretch a string, and then to specifically replace chunks of it with the value that I really want:

* operation pre-stretch - assume that fixed length ws is 5 chars
* must do this 5+1
  describe table i_row lines w_rows.
* mark beginning and end of string
  w_blank+0(1) = 'x'.
  w_blank+5(1) = 'x'.
  do w_rows times.
    concatenate w_stringdata w_blank into w_stringdata.

* now replace the sections of the string with the real data
  loop at i_rows into w_row.
    REPLACE SECTION offset w_cnt LENGTH 5 OF w_stringdata WITH w_row.
    w_cnt = w_cnt + 5.
or - I did this:
* see other data declarations from above
  astring type string,
  fname(100) type c value '/tmp/shimmy.txt'.

  open dataset fname for output in binary mode.
  loop at i_rows into w_row.
    transfer w_row to fname.
  close dataset fname.

  open dataset fname for input in binary mode.
  read dataset fname into astring.
  close dataset fname.

This was trialed on NW4 test drive edition, and Enterprise 4.7.
I'd be really interested to hear of any other solutions that people have - or someone pointing out a glaringly obvious mistake that I've made? And for extra points - figure out why I had to pre-stretch the string with a size greter than the sum total of parts.

Posted by PiersHarding at June 3, 2005 10:06 AM


I download a file which has a string of 10 size but
the data is only 5 chars.
In the downloaded text file the cursor position ends at 5 i want the cursor position shall end at 10.

Posted by: AKSHAY BIRAJDAR at August 10, 2005 1:16 PM