Skip to Content
author's profile photo Former Member
Former Member

String Problem

str1 = Invoice Reference Number: 111, 222, 333, 444

I need to get all the numeric data and set it in the variables .

(variables will have the value of the trimmed numbers).

var1 = 111

var2 = 222

var3 = 333

var4 = 444

kindly take note that number of variables may depend on the numbers in the string.

given above, code sets 4 variables(var1, var2, var3 & var4) because there is 4 combination of numbers.

how to do this in code? is it possible to declare variables on looping?

any help is greatly appreciated.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Nov 24, 2007 at 03:39 AM

    You can tri with this logic:

    REPORT  ZTEST_NP_123.
    
    DATA: STR TYPE STRING.
    
    STR = 'Invoice Reference Number: 111, 222, 333, 444'.
    
    DATA: L_PART1 TYPE STRING,
          L_PART2 TYPE STRING.
    
    DATA: BEGIN OF IT_NO OCCURS 0,
          NUM(10),
          END   OF IT_NO.
    
    SPLIT STR AT ':' INTO L_PART1 L_PART2.
    
    SPLIT L_PART2 AT ',' INTO TABLE IT_NO.
    
    loop at it_no.
      write: / it_no-num.
    endloop.

    Regards,

    Naimesh Patel

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Nov 24, 2007 at 03:57 AM

    Hi Newbie,

    You can not extend variables in dynamic way but we can achieve this using the help of internal tables.

    If your string contains a single common letter which is separating numerics then SPLIT will be used, otherwise string contains different characters in the string which are separating numerics then use the following code.

    Ofcourse, this code can be used for both of the cases, but will be relevant for different characters in the string, otherwise SPLIT will be best option.

    Check this code.

    TYPES: BEGIN OF TY_VAR,

    NO(10),

    END OF TY_VAR.

    DATA IT_VAR TYPE STANDARD TABLE OF TY_VAR WITH HEADER LINE.

    DATA V_LENGTH TYPE I.

    DATA V_FLAG.

    DATA V_POS TYPE I.

    DATA V_CHAR.

    DATA WA_VAR TYPE TY_VAR.

    PARAMETERS: P_NUMBER TYPE STRING.

    START-OF-SELECTION.

    V_LENGTH = STRLEN( P_NUMBER ).

    DO V_LENGTH TIMES.

    V_CHAR = P_NUMBER+V_POS(1).

    IF V_CHAR CO '0123456789'.

    CONCATENATE WA_VAR V_CHAR INTO WA_VAR.

    ELSE.

    CLEAR WA_VAR.

    V_FLAG = 'X'.

    ENDIF.

    IF WA_VAR IS NOT INITIAL AND V_FLAG = 'X'.

    APPEND WA_VAR TO IT_VAR.

    CLEAR WA_VAR.

    CLEAR V_FLAG.

    ENDIF.

    ENDDO.

    *-Numbers in the string

    LOOP AT IT_VAR.

    WRITE:/ IT_VAR-NO.

    ENDLOOP.

    Thanks,

    Vinay

    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.