Skip to Content

correcting chars

Hi all

I'm getting a field of type char(12) which can have not allowed characters. I would like to correct them by changing them in to _ Allowed characters are !"%&'()*+,-./:;<=>?_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ. Performance is cruicial, because this is done for each data set out of up to Millions each day.

I've thought about converting this field into internal table and use modify with a lot of where conditions, but I'm not sure is it good method because each time I would create little tables (12 rows) which may take a lot of time.

Anybody know to solve this problem ?

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Feb 19, 2007 at 12:50 PM

    Look at the CN option of the IF statement, and the Translate statement.

    MattG.

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Marcin Cholewczuk

      The point I was trying to get across was, the solution depend on the 'data'. In your case, there are only a few invalid characters and they occur rarely, and your string is only 12 long.

      If your situation was; a few invalid characters and they occur frequently. Then I would just use TRANSLATE, and allow SAP to work out the best algorithm.

      If your situation was; invalid characters occur in-frequently, but your string was long (at least 20). Then I would look at a DO loop with a IF CN to find the next invalid character, manage the search area.

      MattG.

  • author's profile photo Former Member
    Former Member
    Posted on Feb 20, 2007 at 12:17 PM

    First build a string of all allowed characters. You will need to use hexadecimal numbers to add the unusable characters like ' and ".

    I've written a short program in which I only added the ' this way.

    Then build a string containing ALL characters that are NOT allowed. And have each of these characters followed by an underscore. This string will be used to translate the incoming value.

    Basically, using this method, the incoming strings can be quickly translated using the TRANSLATE statement. That's just a very small piece of code at the end.

    Preparation (of the check strings) will only need to be done once, in the beginning.

    I think this is the quickest way... Took some puzzeling though. 😊

    P.S. Hex is actually Decimal. Strange but true. So if you look at a Hexadecimal character sheet, be sure to look at the correct values!

    <b>EXAMPLE:</b>

    REPORT zbc_temp.
    
    * Data declarations:
    DATA: string1(12) VALUE '1 3$567$@0AB'. "Incorrect value!
    
    * Hexadecimal code to be able to add it to the string:
    DATA: z_hex TYPE x.
    DATA: z_hyphen TYPE x VALUE '27'.
    
    DATA: allowed_chars TYPE string.
    DATA: not_allowed_chars TYPE string.
    DATA counter TYPE i VALUE 32.           "Hex value for SPACE
    
    * Build string with allowed characters:
    CONCATENATE z_hyphen '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' INTO
            allowed_chars.
    
    * Build string with characters that are not allowed (followed by '_').
    * Characters that should be evaluated range from 33 (= '!') up to and
    * including 126 (= '~').
    DO.
    * Last character from hex code page that is checked is hex 126
      IF counter > 126.
        EXIT.            "No more checks need to be done... Exit DO loop.
      ENDIF.
      MOVE counter TO z_hex.
    * Check if the character is allowed
      IF allowed_chars NS z_hex.
        IF counter = 32. "SPACE
    *     Special treatment. SPACE gets removed during the other
    *     concatenate.
          CONCATENATE not_allowed_chars ' _' INTO not_allowed_chars.
        ELSE.
    *   This character is NOT allowed, add to 'not_allowed_chars'
          CONCATENATE not_allowed_chars z_hex '_' INTO not_allowed_chars.
        ENDIF.
      ENDIF.
      counter = counter + 1.
    ENDDO.
    
    * Now all allowed and non-allowed characters are stored in two strings.
    WRITE: / 'Allowed characters: ', allowed_chars.
    WRITE: / 'NOT Allowed characters: ', not_allowed_chars.
    
    * Here the actual values of the incoming parameters are checked and
    * corrected:
    IF string1 CO allowed_chars.
      WRITE: / string1, ' is okay'.
    ELSE.
      WRITE: / string1, ' is not okay'.
      TRANSLATE string1 USING not_allowed_chars.
      WRITE: / string1, ' but corrected'.
    ENDIF.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.