Splitting a string into respective fields of dynamic internal table

Hi,
    I've a string concatenated with a separator. I've to split the string and assign it to the respective fields of an internal table, which is dynamic.
Table name will be passed through selection screen. The data is coming from another system via RFC.
Eg : String ITAB :
                            100;89001;EN;Material1;MATERIAL1
                            100;89002;EN;Material2;MATERIAL2
The String ITAB may contain any master data. Let's say the above data is from MAKT table. So, I want to assign the above data to the respective fields of MAKT internal table(Dynamic).
I heard, this requirement can be achieved using some standard CLASS.
Please help me in doing this task.
Regards,
Sunny

Hello,
you can use dynamic programming for this issue, i.e.:
DATA: gv_table_name   TYPE string,
      gr_type_desc    TYPE REF TO cl_abap_typedescr,
      gr_struct_desc  TYPE REF TO cl_abap_structdescr,
      gr_table_desc   TYPE REF TO cl_abap_tabledescr,
      gv_t            TYPE c,
      gv_comp         TYPE i,
      gr_table_ref    TYPE REF TO data,
      gr_struc_ref   TYPE REF TO data.
DATA: gt_itab   TYPE TABLE OF string,
      gt_split  TYPE TABLE OF string,
      gv_str    TYPE string.
FIELD-SYMBOLS: <table>    TYPE ANY TABLE,
               <struct>   TYPE ANY,
               <comp>     TYPE ANY.
APPEND '100;89001;EN;Material1;MATERIAL1' TO gt_itab.
APPEND '100;89002;EN;Material2;MATERIAL2' TO gt_itab.
"go!
gv_table_name = 'MAKT'.
cl_abap_tabledescr=>describe_by_name(
      EXPORTING p_name = gv_table_name
      RECEIVING p_descr_ref = gr_type_desc
      EXCEPTIONS type_not_found = 4 ).
gr_struct_desc ?= gr_type_desc.
gr_table_desc = cl_abap_tabledescr=>create( gr_struct_desc ).
CREATE DATA gr_table_ref TYPE HANDLE gr_table_desc.
CREATE DATA gr_struc_ref TYPE HANDLE gr_struct_desc.
ASSIGN gr_table_ref->* TO <table>.
ASSIGN gr_struc_ref->* TO <struct>.
DESCRIBE FIELD <struct> TYPE gv_t COMPONENTS gv_comp.
LOOP AT gt_itab INTO gv_str.
  CLEAR: gt_split.
  SPLIT gv_str AT ';' INTO TABLE gt_split.
  DO gv_comp TIMES.
    READ TABLE gt_split INTO gv_str INDEX sy-index.
    ASSIGN COMPONENT sy-index OF STRUCTURE <struct> TO <comp>.
    <comp> = gv_str.
    CLEAR gv_str.
  ENDDO.
  INSERT <struct> INTO TABLE <table>.
ENDLOOP.
After this code you will have all data in <table> field symbol in proper type.
Regards,
Jacek

Similar Messages

  • To populate values into single field in an internal table

    Hi Friends,
    How we need to populate values into single field in an internal table.
    E.g itab consits of single field ( name)
           i need to assign values to this field name .like
          peter,
          john,
          abrahm,
          daneyal
    Pls tell me i how i need to code for this
    Thanks ,
    Parnith

    Hi,
    Please look at the below code :
    DATA : BEGIN OF itab OCCURS 0 ,
             name(20) TYPE c,
           END OF itab.
    START-OF-SELECTION.
      itab-name = 'Peter'.
      APPEND itab.
      CLEAR itab.
      itab-name = 'John'.
      APPEND itab.
      CLEAR itab.
      itab-name = 'Abrahm'.
      APPEND itab.
      CLEAR itab.
      itab-name = 'Daneyal'.
      APPEND itab.
      CLEAR itab.
      LOOP AT itab.
        WRITE : / itab.
      ENDLOOP.
    Thanks,
    Sriram Ponna.

  • At end of field,  for dynamic internal table entries ?

    Dear All,
    I've data in my dynamic internal table.
    But, to understand in a better way I created a test program with static entries.
    Case 1 -->
    DATA: BEGIN OF itab OCCURS 0,
            year TYPE char4,
            name TYPE char10,
          END OF itab.
    DATA: BEGIN OF itab2 OCCURS 0 ,
            year TYPE char4,
            name TYPE char10,
            count TYPE i,
          END OF itab2.
    DATA: gv_count TYPE i,
          gv_flag TYPE char1.
    itab-name = 'AAAA'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'AAAB'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'AAAC'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'AAAD'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBB'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBA'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBC'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBD'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCC'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCA'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCB'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCD'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCE'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCf'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDD'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDA'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDB'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDC'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDE'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    SORT itab BY year.
    LOOP AT itab.
    * Counter
      gv_count = gv_count + 1.
      AT END OF year.
        itab2-count = gv_count.
        MOVE-CORRESPONDING itab TO itab2.
        APPEND itab2.
        CLEAR gv_count.
        WRITE:/ itab2-name,
        itab2-year,
        itab2-count.
        CLEAR itab2.
        CLEAR gv_flag.
      ENDAT.
    ENDLOOP.
    Now for the above program the Output is :
    Name     Year          Count
    ********** 2005          5
    ********** 2006          6
    ********** 2007          4
    ********** 2008          4
    " Forget about the asterisk values as it's an example !
    The above output is correct, But in my real case the table structure is not like the above .
    The order of the field are in reverse.
    i.e. The internal table is like this :
    Case 2 -->
    DATA: BEGIN OF itab OCCURS 0,
            name TYPE char10,
            year TYPE char4,
          END OF itab.
    DATA: BEGIN OF itab2 OCCURS 0 ,
            name TYPE char10,
            year TYPE char4,
            count TYPE i,
          END OF itab2.
    In this case if I'm using at end of year the output is coming wrong.
    Even on change of is also not working.
    Request you guys to help me out so that I can achieve the same output as of case 1 with the table declaration as of CASE 2.
    Regards,
    Deepu.k

    Hello Rich Heilman,
    Again you came for my rescue ............
    My code for this scenario is like this :
    DATA: BEGIN OF itab OCCURS 0,
            name TYPE char10,
            year TYPE char4,
          END OF itab.
    DATA: BEGIN OF itab2 OCCURS 0 ,
            name TYPE char10,
            year TYPE char4,
            count TYPE i,
          END OF itab2.
    DATA: gv_count TYPE i,
          gv_flag TYPE char1.
    itab-name = 'AAAA'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'AAAB'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'AAAC'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'AAAD'.
    itab-year = '2008'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBB'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBA'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBC'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'BBBD'.
    itab-year = '2007'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCC'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCA'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCB'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCD'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCE'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'CCCf'.
    itab-year = '2006'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDD'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDA'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDB'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDC'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    itab-name = 'DDDE'.
    itab-year = '2005'.
    APPEND itab.
    CLEAR itab.
    SORT itab BY year.
    LOOP AT itab.
    * Counter
      gv_count = gv_count + 1.
      IF sy-tabix GT 1.
        ON CHANGE OF itab-year.
          itab2-count = gv_count.
          MOVE-CORRESPONDING itab TO itab2.
          APPEND itab2.
          CLEAR gv_count.
          WRITE:/ itab2-name,
          itab2-year,
          itab2-count.
          CLEAR itab2.
          CLEAR gv_flag.
        ENDON.
      ENDIF.
    ENDLOOP.
    The Output is :
    DDDC       2005          2
    CCCf       2006          4
    BBBD       2007          6
    AAAD       2008          4
    The above output is wrong.
    Can u correct me where I'm doing wrong ?
    Regards,
    Deepu.K

  • How to select into a field of my internal table?

    Howdy,
    I have an internal table:
    TYPES: BEGIN OF T_OUTPUT,
            EQUIPMENT         TYPE  EQUI-EQUNR,
            DESCRIPTION       TYPE  EQKT-EQKTX,
            EQUIPMENT_CAT     TYPE  EQUI-EQTYP,
            MASTER_WARRANTY    TYPE  BGMKOBJ-MGANR,
            DELIVERY           TYPE  LIKP-VBELN,
    END OF T_OUTPUT.
    DATA: ITAB_DETAILS TYPE STANDARD TABLE OF T_OUTPUT WITH HEADER LINE.
    Now i'd like to do a slect from LIKP into the field ITAB_DETAILS-delivery, but his code doesn't work?
    SELECT VBELN FROM LIKP INTO table ITAB_details-delivery
             WHERE  VBELN IN S_VBELN
             AND VKORG  = P_VKORG
             AND WERKS  = P_WERKS
             AND ERDAT IN S_ERDAT
             AND LFART IN S_LFART.
    Does anyone know what I am doing wrong?
    I need to keep the name of the field as 'delivery' and I don't want to change its order in the internal table - Otherwise I'd ahve used the 'MOVE CORRESPONDING' command.
    Does anyone have any ideas?
    Thanks!

    STEVE,
    Modified Code:
    Get value for Delivery
       SELECT <b>SINGLE</b> VBELN FROM LIKP
           INTO ITAB_details-delivery
             WHERE  VBELN IN S_VBELN
             AND VKORG  = P_VKORG
             AND WERKS  = P_WERKS
             AND ERDAT IN S_ERDAT
             AND LFART IN S_LFART.
    Insert into Internal Table ASSUMING u have values for
    other fields in the Work Area.
    <b>  append ITAB_DELIVERY.</b>
    Thanks
    Kam

  • Filling dynamic internal table with data from other internal table

    Hi Friends,
    My problem is that i have already built a dynamic internal table
    (class int_table->create) but now i want to fill it with data from other internal table.
    The dynamic table column name and the field value of the data filled internal table are same, but how to access that column name, since i cant hard code it anyway.
    Like if my werks field value is '8001'. I want to place it under the column 8001 of dynamic table, Can anybody help me in this regard?
    Awarding points is not a problem for even giving a slight hint.
    Best Regards

    Hi
    See this
    Dynamic internal table is internal table that we create on the fly with flexible column numbers.
    For sample code, please look at this code tutorial. Hopefully it can help you
    Check this link:
    http://www.****************/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm
    Sample code:
    DATA: l_cnt(2) TYPE n,
    l_cnt1(3) TYPE n,
    l_nam(12),
    l_con(18) TYPE c,
    l_con1(18) TYPE c,
    lf_mat TYPE matnr.
    SORT it_bom_expl BY bom_comp bom_mat level.
    CLEAR: l_cnt1, <fs_dyn_wa>.
    Looping the component internal table
    LOOP AT it_bom_expl INTO gf_it_bom_expl.
    CLEAR: l_cnt1.
    AT NEW bom_comp.
    CLEAR: l_cnt, <fs_dyn_wa>, lf_mat.
    For every new bom component the material data is moved to
    temp material table which will be used for assigning the levels
    checking the count
    it_mat_temp[] = it_mat[].
    Component data is been assigned to the field symbol which is checked
    against the field of dynamic internal table and the value of the
    component number is been passed to the dynamic internal table field
    value.
    ASSIGN COMPONENT c_comp_list OF STRUCTURE <fs_dyn_wa> TO
    <fs_check>.
    <fs_check> = gf_it_bom_expl-bom_comp.
    ENDAT.
    AT NEW bom_mat.
    CLEAR l_con.
    ENDAT.
    lf_mat = gf_it_bom_expl-bom_mat.
    Looping the temp internal table and looping the dynamic internal table
    *by reading line by line into workarea, the materialxxn is been assigned
    to field symbol which will be checked and used.
    LOOP AT it_mat_temp.
    l_nam = c_mat.
    l_cnt1 = l_cnt1 + 1.
    CONCATENATE l_nam l_cnt1 INTO l_nam.
    LOOP AT <fs_dyn_table2> ASSIGNING <fs_dyn_wa2>.
    ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa2> TO <fs_xy>.
    ENDLOOP.
    IF <fs_xy> = lf_mat.
    CLEAR lf_mat.
    l_con1 = l_con.
    ENDIF.
    Checking whether the material exists for a component and if so it is
    been assigned to the field symbol which is checked against the field
    of dynamic internal table and the level of the component number
    against material is been passed to the dynamic internal table field
    value.
    IF <fs_xy> = gf_it_bom_expl-bom_mat.
    ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa> TO <fs_check>.
    CLEAR l_con.
    MOVE gf_it_bom_expl-level TO l_con.
    CONCATENATE c_val_l l_con INTO l_con.
    CONDENSE l_con NO-GAPS.
    IF l_con1 NE space.
    CONCATENATE l_con1 l_con INTO l_con SEPARATED BY c_comma.
    CLEAR l_con1.
    l_cnt = l_cnt - 1.
    ENDIF.
    <fs_check> = l_con.
    l_cnt = l_cnt + 1.
    ENDIF.
    ENDLOOP.
    AT END OF bom_comp.
    At end of every new bom component the count is moved to the field
    symbol which is checked against the field of dynamic internal table
    and the count is been passed to the dynamic internal table field
    value.
    ASSIGN COMPONENT c_count OF STRUCTURE <fs_dyn_wa> TO <fs_check>.
    <fs_check> = l_cnt.
    INSERT <fs_dyn_wa> INTO TABLE <fs_dyn_table>.
    ENDAT.
    ENDLOOP.
    Reward if useful
    Anji

  • Dynamic Internal Table with the same name

    Hey Guys
    I have a question.
    I know that we can create dynamic internal table taking a struct dynamically.
    But I have 2 ques on the same subject.
    1. Can we create an internal table based on a type that we have locally declared eg
    types: begin of ty_demo.
               var1(1) type c,
               var2     type p,
              end of ty_demo.
    2. If an internal table is already declared based on the above type say data: i_tab type standard table of ty_demo.
        If i need to enhance the struct of this internal table. Can i do that by dynamically redefining it based on a different structure but keepin the same name i_tab.
    In a nut shell I cannot change the name of my itab but I want to enhance the structure.
    I Hope I am clear.
    Help will be greatly apprcieated.
    Thanks
    Sameer

    hai.
    we can create an internal table based on a type that we have locally declared, but you have to use data instead of types like.
    data: begin of ty_demo.
    var1(1) type c,
    var2 type p,
    end of ty_demo.
    check the example notes for dynamic itab .
    Dynamic internal table is internal table that we create on the fly with flexible column numbers.
    For sample code, please look at this code tutorial. Hopefully it can help you
    Check this link:
    http://www.****************/Tutorials/ABAP/DynamicInternaltable/DynamicInternalTable.htm
    Sample code:
    DATA: l_cnt(2) TYPE n,
    l_cnt1(3) TYPE n,
    l_nam(12),
    l_con(18) TYPE c,
    l_con1(18) TYPE c,
    lf_mat TYPE matnr.
    SORT it_bom_expl BY bom_comp bom_mat level.
    CLEAR: l_cnt1, <fs_dyn_wa>.
    Looping the component internal table
    LOOP AT it_bom_expl INTO gf_it_bom_expl.
    CLEAR: l_cnt1.
    AT NEW bom_comp.
    CLEAR: l_cnt, <fs_dyn_wa>, lf_mat.
    For every new bom component the material data is moved to
    temp material table which will be used for assigning the levels
    checking the count
    it_mat_temp[] = it_mat[].
    Component data is been assigned to the field symbol which is checked
    against the field of dynamic internal table and the value of the
    component number is been passed to the dynamic internal table field
    value.
    ASSIGN COMPONENT c_comp_list OF STRUCTURE <fs_dyn_wa> TO
    <fs_check>.
    <fs_check> = gf_it_bom_expl-bom_comp.
    ENDAT.
    AT NEW bom_mat.
    CLEAR l_con.
    ENDAT.
    lf_mat = gf_it_bom_expl-bom_mat.
    Looping the temp internal table and looping the dynamic internal table
    *by reading line by line into workarea, the materialxxn is been assigned
    to field symbol which will be checked and used.
    LOOP AT it_mat_temp.
    l_nam = c_mat.
    l_cnt1 = l_cnt1 + 1.
    CONCATENATE l_nam l_cnt1 INTO l_nam.
    LOOP AT <fs_dyn_table2> ASSIGNING <fs_dyn_wa2>.
    ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa2> TO <fs_xy>.
    ENDLOOP.
    IF <fs_xy> = lf_mat.
    CLEAR lf_mat.
    l_con1 = l_con.
    ENDIF.
    Checking whether the material exists for a component and if so it is
    been assigned to the field symbol which is checked against the field
    of dynamic internal table and the level of the component number
    against material is been passed to the dynamic internal table field
    value.
    IF <fs_xy> = gf_it_bom_expl-bom_mat.
    ASSIGN COMPONENT l_nam OF STRUCTURE <fs_dyn_wa> TO <fs_check>.
    CLEAR l_con.
    MOVE gf_it_bom_expl-level TO l_con.
    CONCATENATE c_val_l l_con INTO l_con.
    CONDENSE l_con NO-GAPS.
    IF l_con1 NE space.
    CONCATENATE l_con1 l_con INTO l_con SEPARATED BY c_comma.
    CLEAR l_con1.
    l_cnt = l_cnt - 1.
    ENDIF.
    <fs_check> = l_con.
    l_cnt = l_cnt + 1.
    ENDIF.
    ENDLOOP.
    AT END OF bom_comp.
    At end of every new bom component the count is moved to the field
    symbol which is checked against the field of dynamic internal table
    and the count is been passed to the dynamic internal table field
    value.
    ASSIGN COMPONENT c_count OF STRUCTURE <fs_dyn_wa> TO <fs_check>.
    <fs_check> = l_cnt.
    INSERT <fs_dyn_wa> INTO TABLE <fs_dyn_table>.
    ENDAT.
    ENDLOOP.
    regards.
    sowjanya.b

  • Dynamic internal table operations

    Hi,
    I have an issue with dynamic internal table wherein I need to write  a  select statement with For All Entries on dynamic internal table and populating the record into another dynamic internal table.
    I am trying with the code below but its not working.
    SELECT (FLD)
    FROM (T_TABLE)
    INTO CORRESPONDING FIELDS OF TABLE <DYN1_TABL>
    FOR ALL ENTRIES IN <DYN_TABL>
    WHERE KEY_FNAME = <DYN_TABL>-KEY_FNAME.
    FLD is the dynamic field.
    T_TABLE is the dynamic database table.
    DYN1_TABL is the dynamic internal table
    DYN_TABL is the dynamic internal table
    KEY_FNAME is the dynamic key field of T_TABLE.
    Pls help me on this issue.
    Response with code snippets will be appreciated.
    Thanks in advance.

    Thanks for your reply Nikki but my issue is to reference  a field in dynamic internal table to be used with 'For All Entries' satement.
    like for static we use
    For All Entries in Itab where <fieldname> = itab-<fieldname>
    I was looking for the same statement for dynamic tables

  • Concatenate Primary Key Fields of an internal table into a String.

    Hi,
    I have a dynamic internal table created.
    In the dynamic internal table,
    How to concatenate all the primary keys of that row concatenated into a string and non primary keys into a different string.
    Can some one help me on this.
    E.g:
    Itab1 has 3 Primary fields and 3 Non primary key fields and it has 2 records.
    Table     col1     col2     col3     col4     col5     col6
    Row1     AA     AB     AC     1     2     3
    Row2     ZA     ZB     ZC     1     2     3
    The following is needed.
    Itab2 will have the concatenate strings
                       Key                               Nkey
    Row1        AAABAC                      010203
    Row2        ZAZBZC                       010203
    Thanks,
    Chaithanya

    C,
    Try something like this -  Use an ASSIGN in a LOOP:
      data: lv_PK type string.
      data: lv_non_PK type string.
      field-symbols: lv_str type string.
      assign local copy of initial line of gt_table to <ls_table>.
      loop at gt_table.  " this is your table.
        do n times.  " n = 6 in your case
          if n < 4.
             assign component sy-index of structure <ls_table> to <lv_str>.
             concatenate <lv_str> to lv_PK.
       else.
             assign component sy-index of structure <ls_table> to <lv_str>.
             concatenate <lv_str> to lv_Non_PK.
         endif.
        enddo.
        append <ls_table> to <gt_table>.
      endloop.

  • OR ('|') in regular expressions (e.g. split a String into lines)

    Which match gets used when you use OR ('|') to specify multiple possible matches in a regex, and there are multiple matches among the supplied patterns? The first one (in the order written) which matches? Or the one which matches the most characters?
    To make this concrete, suppose that you want to split a String into lines, where the line delimiters are the same as the [line terminators used by Java regex|http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#lt] :
         A newline (line feed) character ('\n'),
         A carriage-return character followed immediately by a newline character ("\r\n"),
         A standalone carriage-return character ('\r'),
         A next-line character ('\u0085'),
         A line-separator character ('\u2028'), or
         A paragraph-separator character ('\u2029)
    This problem has [been considered before|http://forums.sun.com/thread.jspa?forumID=4&threadID=464846] .
    If we ignore the idiotic microsoft two char \r\n sequence, then no problem; the Java code would be:
    String[] lines = s.split("[\\n\\r\\u0085\\u2028\\u2029]");How do we add support for \r\n? If we try
    String[] lines = s.split("[\\n\\r\\u0085\\u2028\\u2029]|\\r\\n");which pattern of the compound (OR) regex gets used if both match? The
    [\\n\\r\\u0085\\u2028\\u2029]or the
    \\r\\n?
    For instance, if the above code is called when
    s = "a\r\nb";and if the first pattern
    [\\n\\r\\u0085\\u2028\\u2029]is used for the match when the \r is encountered, then the tokens will be
    "a", "", "b"
    because there is an empty String between the \r and following \n. On the other hand, if the rule is use the pattern which matches the most characters, then the
    \\r\\n
    pattern will match that entire \r\n and the tokens will be
    "a", "b"
    which is what you want.
    On my particular box, using jdk 1.6.0_17, if I run this code
    String s = "a\r\nb";
    String[] lines = s.split("[\\n\\r\\u0085\\u2028\\u2029]|\\r\\n");
    System.out.print(lines.length + " lines: ");
    for (String line : lines) System.out.print(" \"" + line + "\"");
    System.out.println();
    if (true) return;the answer that I get is
    3 lines:  "a" "" "b"So it seems like the first listed pattern is used, if it matches.
    Therefore, to get the desired behavior, it seems like I should use
    "\\r\\n|[\\n\\r\\u0085\\u2028\\u2029]"instead as the pattern, since that will ensure that the 2 char sequence is first tried for matches. Indeed, if change the above code to use this pattern, it generates the desired output
    2 lines:  "a" "b"But what has me worried is that I cannot find any documentation concerning this "first pattern of an OR" rule. This means that maybe the Java regex engine could change in the future, which is worrisome.
    The only bulletproof way that I know of to do line splitting is the complicated regex
    "(?:(?<=\\r)\\n)" + "|" + "(?:\\r(?!\\n))" + "|" + "(?:\\r\\n)" + "|" + "\\u0085" + "|" + "\\u2028" + "|" + "\\u2029"Here, I use negative lookbehind and lookahead in the first two patterns to guarantee that they never match on the end or start of a \r\n, but only on isolated \n and \r chars. Thus, no matter which order the patterns above are applied by the regex engine, it will work correctly. I also used non-capturing groups
    (?:X)
    to avoid memory wastage (since I am only interested in grouping, and not capturing).
    Is the above complicated regex the only reliable way to do line splitting?

    bbatman wrote:
    Which match gets used when you use OR ('|') to specify multiple possible matches in a regex, and there are multiple matches among the supplied patterns? The first one (in the order written) which matches? Or the one which matches the most characters?
    The longest match wins, normally. Except for alternation (or) as can be read from the innocent sentence
    The Pattern engine performs traditional NFA-based matching with ordered alternation as occurs in Perl 5.
    in the javadocs. More information can be found in Friedl's book, the relevant page of which google books shows at
    [http://books.google.de/books?id=GX3w_18-JegC&pg=PA175&lpg=PA175&dq=regular+expression+%22ordered+alternation%22&source=bl&ots=PHqgNmlnM-&sig=OcDjANZKl0VpJY0igVxkQ3LXplg&hl=de&ei=Dcg7S43NIcSi_AbX-83EDQ&sa=X&oi=book_result&ct=result&resnum=1&ved=0CA0Q6AEwAA#v=onepage&q=&f=false|http://books.google.de/books?id=GX3w_18-JegC&pg=PA175&lpg=PA175&dq=regular+expression+%22ordered+alternation%22&source=bl&ots=PHqgNmlnM-&sig=OcDjANZKl0VpJY0igVxkQ3LXplg&hl=de&ei=Dcg7S43NIcSi_AbX-83EDQ&sa=X&oi=book_result&ct=result&resnum=1&ved=0CA0Q6AEwAA#v=onepage&q=&f=false]
    If this link does not survive, search google for
    regular expression "ordered alternation"
    My first hit went right into Friedl's book.
    Harald.

  • How to split this string into 4 sections to a max 35 characters

    Hello,
    Does anyone have an idea how I can acheive this please.
    I have this string
    Expense_Inv_8- ExpenseInv_7- Exp001- Expense_Inv_6- Expense_Inv_5- Expense_Inv_4- Expense_Inv_3- Expense_Inv_2- Expense_inv1
    and I need to display them in sections seperated by ';' as explained below
    Section 1 Section 2 Section 3 Section 4
    Expense_Inv_8- ExpenseInv_7- Exp001;Expense_Inv_6- Expense_Inv_5;Expense_Inv_4- Expense_Inv_3;Expense_Inv_2;
    need to split this string into 4 sections seperated by ';' and each section should be of no more than 35 characters, if null should end ;;;
    Section 1, 35 Character ended by;
    Section 2, broken off after Expense_Inv_5 because Expense_Inv_4 will take it over 35 chracters)
    Section 3, should only take Expense_Inv_4- Expense_Inv_3, because adding Expense_Inv_2 will take it over 35
    characters, each record in the string is seperated by '-'
    Section 4, dispays the reminder of the string
    regards
    Ade

    Hi,
    Welcome to the forum!
    Whenever you ask a question, it helps if you post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) and the results you want from that data.
    I think I understand the problemk well enough to attempt a solution, but if the query below isn't right, please post that information.
    WITH     cntr     AS
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL     <= ( SELECT  MAX (LENGTH (txt))
                             FROM    table_x
    ,     got_best_path     AS
         SELECT     id
         ,     txt
         ,     MAX ( SYS_CONNECT_BY_PATH ( TO_CHAR (c.n, '99')
                      ) AS best_path
         FROM     cntr     c
         JOIN     table_x     x     ON     c.n <= LENGTH (x.txt)
         START WITH     c.n     = 1
         CONNECT BY     c.n - PRIOR c.n     BETWEEN  1
                                 AND      :section_length
              AND     x.id          = PRIOR     x.id
              AND     SUBSTR ( x.txt
                                 , c.n
                                 , 1
                                 )     = '-'
         AND     LEVEL          <= :section_cnt
         GROUP BY  id
         ,            txt
    ,     got_pos     AS
         SELECT     id
         ,     REPLACE ( txt
                   ) || ';'                         AS txt
         ,     best_path
         ,     TO_NUMBER (REGEXP_SUBSTR (best_path, '[0-9]+', 1, 2))     AS pos_2
         ,     TO_NUMBER (REGEXP_SUBSTR (best_path, '[0-9]+', 1, 3))     AS pos_3
         ,     TO_NUMBER (REGEXP_SUBSTR (best_path, '[0-9]+', 1, 4))     AS pos_4
         FROM     got_best_path
    SELECT  id
    ,     SUBSTR (txt,     1    , NVL ( pos_2         , :section_length))     AS section_1
    ,     SUBSTR (txt, pos_2 + 1, NVL ((pos_3 - pos_2), :section_length))     AS section_2
    ,     SUBSTR (txt, pos_3 + 1, NVL ((pos_4 - pos_3), :section_length))     AS section_3
    ,     SUBSTR (txt, pos_4 + 1,                        :section_length )     AS section_4
    FROM     got_pos
    ;As written, this requires SQL*Plus 9 (or higher). You can have multiple versions or SQL*Plus on the same client, if you really need to keep the older version.
    :section_length is the maximum length of each section (35, as you stated the problem).
    :section_cnt is the number of sections. In the query above, this is 4. If you change it, you not only have to change the bind variable, but you have to change the hard-coded SELECT clauses of the main query and the last sub-query (that is, got_pos).
    MODEL or PL/SQL would probably be better ways to solve this problem.

  • Splitting a string into 4 equal parts

    Hi All,
    I have a string with maximum no of characters as 100,
    I want to split this string into 4 equal parts.
    Any help will be appreciated.
    Regards

    Hi Rajeev,
    Use this sample code
    class SplitString {
           public static void main(String[] arguments) {
              StringTokenizer ex1, ex2; // Declare StringTokenizer Objects
              int count = 0;
              String strOne = "one two  three      four   five";
              ex1 = new StringTokenizer(strOne); //Split on Space (default)
              while (ex1.hasMoreTokens()) {
                  count++;
                  wdComponentAPI.getMessageManager().reportSuccess("Token " + count + " is" +    ex1.nextToken() );
              count = 0;  // Reset counter
              String strTwo = "item one,item two,item three,item four"; // Comma Separated
              ex2 = new StringTokenizer(strTwo, ",");  //Split on comma
              while (ex2.hasMoreTokens()) {
                  count++;
                  wdComponentAPI.getMessageManager().reportSuccess("Token " + count + " is "+ ex2.nextToken() );
    Thanks
    Anup
    Edited by: Anup Bharti on Oct 27, 2008 12:36 PM

  • How to add new field into dynamic internal table

    Hello Expert.
    how to add new field into dynamic internal table.
    PARAMETERS: P_TABLE(30).    "table name
    DATA: I_TAB TYPE REF TO DATA.
    FIELD-SYMBOLS: <TAB> TYPE standard TABLE.
    *Create dynamic FS
    create DATA I_TAB TYPE TABLE OF (p_table).
      ASSIGN I_TAB->* TO <TAB>.
    SELECT * FROM (p_table) INTO TABLE <TAB>.
       here i want to add one more field into <TAB> at LAST position and my 
       Field name  =  field_stype     and
       Field type    =  'LVC_T_STYL'
    could you please helpme out .

    Hi,
    Please find the code below.You can add the field acc to your requirement.
    Creating Dynamic internal table
    TYPE-POOLS: slis.
    FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE,  u201C Dynamic internal table name
                   <fs_dyntable>,                     u201C Field symbol to create work area
                   <fs_fldval> type any.              u201C Field symbol to assign values 
    PARAMETERS: p_cols(5) TYPE c.                     u201C Input number of columns
    DATA:   t_newtable TYPE REF TO data,
            t_newline  TYPE REF TO data,
            t_fldcat   TYPE slis_t_fldcat_alv,
            t_fldcat   TYPE lvc_t_fcat,
            wa_it_fldcat TYPE lvc_s_fcat,
            wa_colno(2) TYPE n,
            wa_flname(5) TYPE c. 
    Create fields .
      DO p_cols TIMES.
        CLEAR wa_it_fldcat.
        move sy-index to wa_colno.
        concatenate 'COL'
                    wa_colno
               into wa_flname.
        wa_it_fldcat-fieldname = wa_flname.
        wa_it_fldcat-datatype = 'CHAR'.
        wa_it_fldcat-intlen = 10.
        APPEND wa_it_fldcat TO t_fldcat.
      ENDDO. 
    Create dynamic internal table and assign to FS
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = t_fldcat
        IMPORTING
          ep_table        = t_newtable. 
      ASSIGN t_newtable->* TO <t_dyntable>. 
    Create dynamic work area and assign to FS
      CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
      ASSIGN t_newline->* TO <fs_dyntable>.
    Populating Dynamic internal table 
      DATA: fieldname(20) TYPE c.
      DATA: fieldvalue(10) TYPE c.
      DATA: index(3) TYPE c. 
      DO p_cols TIMES. 
        index = sy-index.
        MOVE sy-index TO wa_colno.
        CONCATENATE 'COL'
                    wa_colno
               INTO wa_flname. 
    Set up fieldvalue
        CONCATENATE 'VALUE' index INTO
                    fieldvalue.
        CONDENSE    fieldvalue NO-GAPS. 
        ASSIGN COMPONENT  wa_flname
            OF STRUCTURE <fs_dyntable> TO <fs_fldval>.
        <fs_fldval> =  fieldvalue. 
      ENDDO. 
    Append to the dynamic internal table
      APPEND <fs_dyntable> TO <t_dyntable>.
    Displaying dynamic internal table using Grid. 
    DATA: wa_cat LIKE LINE OF fs_fldcat. 
      DO p_cols TIMES.
        CLEAR wa_cat.
        MOVE sy-index TO wa_colno.
        CONCATENATE 'COL'
                    wa_colno
               INTO wa_flname. 
        wa_cat-fieldname = wa_flname.
        wa_cat-seltext_s = wa_flname.
        wa_cat-outputlen = '10'.
        APPEND wa_cat TO fs_fldcat.
      ENDDO. 
    Call ABAP List Viewer (ALV)
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          it_fieldcat = fs_fldcat
        TABLES
          t_outtab    = <t_dyntable>.

  • Dynamic Internal Table with Dynamic Fields

    Hi all,
    My scenario is fairly simple----
    --> End user clicks a button on screen and he gets the list of HR tables.
    --> Then selects a table and list of all the fields for that table gets displayed.
    --> He/she selects the fields they want data to be retrieved for.
    So, the requirement is, the dynamic internal table should get created with the fields selected.
    The select statement should only retrieve fields which were selected by the end user
    and from the table selected by the end user.
    I  believe the fields selected by end user can be passed by a variable of  type string.
    something like this---
    select (fields)
    from (p_table)        " Table selected by end user
    into  dynamic internal table.   " should contain columns selected by end user"
    Appreciate your inputs and guidance.
    Warm regards,
    Hari Kiran

    TYPE-POOLS :ABAP.
    Parameters P_TAB      TYPE        DDOBJNAME.
    DATA  : GO_LINE_TYPE  TYPE REF TO CL_ABAP_STRUCTDESCR,
            GO_TABLE_DESC TYPE REF TO CL_ABAP_TABLEDESCR,
            GS_COMPONENTS TYPE        ABAP_COMPONENTDESCR,
            GT_COMPONENTS TYPE        ABAP_COMPONENT_TAB,
            GR_TAB        TYPE REF TO DATA,
            GT_FIELDS  TYPE TABLE OF DFIES WITH HEADER LINE.
    FIELD-SYMBOLS: <GT_TABLE> TYPE TABLE,
                   <GS_TABLE> TYPE ANY,
                   <GV_VALUE> TYPE ANY.
    CALL FUNCTION 'DDIF_FIELDINFO_GET'
      EXPORTING
        TABNAME              = P_TAB
    *   FIELDNAME            = ' '
    *   LANGU                = SY-LANGU
    *   LFIELDNAME           = ' '
    *   ALL_TYPES            = ' '
    *   GROUP_NAMES          = ' '
    *   UCLEN                =
    * IMPORTING
    *   X030L_WA             =
    *   DDOBJTYPE            =
    *   DFIES_WA             =
    *   LINES_DESCR          =
    TABLES
        DFIES_TAB            =  GT_FIELDS
    *   FIXED_VALUES         =
    EXCEPTIONS
       NOT_FOUND            = 1
       INTERNAL_ERROR       = 2
       OTHERS               = 3.
    IF SY-SUBRC <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    LOOP AT GT_FIELDS.
      CLEAR GS_COMPONENTS.
      GS_COMPONENTS-NAME  = GT_FIELDS-FIELDNAME.
      GS_COMPONENTS-TYPE ?= CL_ABAP_STRUCTDESCR=>DESCRIBE_BY_NAME( GT_FIELDS-ROLLNAME ).
      APPEND GS_COMPONENTS TO GT_COMPONENTS.
    ENDLOOP.
    GO_LINE_TYPE  = CL_ABAP_STRUCTDESCR=>CREATE( GT_COMPONENTS ).
    GO_TABLE_DESC = CL_ABAP_TABLEDESCR=>CREATE( GO_LINE_TYPE ).
    CREATE DATA:  GR_TAB TYPE HANDLE GO_TABLE_DESC.
    ASSIGN:  GR_TAB->* TO <GT_TABLE>.
    SELECT * FROM (P_TAB) APPENDING CORRESPONDING FIELDS OF TABLE <GT_TABLE>.
    LOOP AT <GT_TABLE> ASSIGNING <GS_TABLE>.
      NEW-LINE.
      DO.
        ASSIGN COMPONENT SY-INDEX OF STRUCTURE <GS_TABLE> TO <GV_VALUE>.
        IF SY-SUBRC NE '0'.
          EXIT.
        ENDIF.
        WRITE : <GV_VALUE>.
      ENDDO.
    ENDLOOP.

  • Building Field-Catalog for dynamic internal tables

    Hi All,
    I  created a dynamic internal table(<fs_final>,<fs_final_line>)  and populated data into it.Now I want to provide field catalog to these dynamic internal table.
    Can anyone suggest me the how to create a field catalog for existing dynamic internal table.Thanks in advance.
    Regards,
    Chakradhar.
    Moderator message - Please use proper subject line in future.
    Message was edited by: Suhas Saha

    Use this code to get all components of internal table.
    Then build fieldcatlog.
    DATA: tab_return type abap_compdescr_tab,
              components like line of tab_return.
    perform get_int_table_fields using    <fs_it> changing  tab_return.
    loop at tab_return into components.
      wa_fldcat-fieldname  = components-name.               "Field Name
      wa_fldcat-scrtext_m  = components-name.              "Column Heading
      wa_fldcat-inttype    = components-type_kind.     "Data Type
      wa_fldcat-intlen      = components-length.          "Data Lenght
      wa_fldcat-decimals = components-decimals.     "Data Decimal Places
    append wa_fldcat to it_fldcat.
    endloop.
    form get_int_table_fields  using    t_data type any table
                                changing t_return type abap_compdescr_tab.
       data:
       oref_table type ref to cl_abap_tabledescr,
       oref_struc type ref to cl_abap_structdescr,
       oref_error type ref to cx_root,
       text type string.
    *Get the description of data object type
       try.
           oref_table ?=
           cl_abap_tabledescr=>describe_by_data( t_data ).
         catch cx_root into oref_error.
           text = oref_error->get_text( ).
           write: / text.
           exit.
       endtry.
    *Get the line type
       try.
           oref_struc ?= oref_table->get_table_line_type( ).
         catch cx_root into oref_error.
           text = oref_error->get_text( ).
           write: / text.
           exit.
       endtry.
       append lines of oref_struc->components to t_return.
    endform.
    Regards
    Sreekanth

  • Popualting data into dynamic internal table

    Hi all,
        I am working on Dynamic internal tables.I have created the structure of internal table but i am unable to populate data into it.Though i have seen some of the forums but those didn't help me. please provide me the solution. Here is my code what i did.I have the data in itab and itab1.
      My requirement is meinh fileds are dynamic.thie meinh and umren are two fileds in mvke table.for one record ihave to populate umren value for that particular meinh value.Pls help me.
    mtart     vkorg     werks     mtanr     Meinh(kg)         Meinh(gl)
    fert     0353     0303     3231     Umren value  Umren value
    REPORT  z123.
                  T Y P E - P O O L S
    TYPE-POOLS :slis,abap.
                  T A B L E S
    *Tables
    TABLES: mara,marm,mvke,marc.
                  F I E L D - S Y M B O L S
    FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
                    <dyn_wa> ,
                    <dyn_field>.
                    I N T E R N A L         T A B L E S
    Internal Tables declaration for ALV GRID/LIST
    DATA: ls_fieldcat TYPE slis_fieldcat_alv,    "FIELD CATALOG LIST
          tb_uom_cat TYPE slis_t_fieldcat_alv,   "FIELD CATALOG
          ls_fieldcat1 TYPE lvc_s_fcat,
          tb_dy_cat TYPE lvc_t_fcat,             "FIELD CATALOG
          tb_outputcat TYPE slis_t_fieldcat_alv,
          tb_events TYPE slis_t_event,
          ref_grid TYPE REF TO cl_gui_alv_grid,
          t_events LIKE LINE OF tb_events.
    DATA: BEGIN OF itab OCCURS 0,
          mtart LIKE mara-mtart,
          vkorg LIKE mvke-vkorg,
          werks LIKE marc-werks,
          matnr TYPE mara-matnr,
          END OF itab.
    DATA: BEGIN OF itab1 OCCURS 0,
          matnr TYPE marm-matnr,
          umren TYPE marm-umren,
          meinh TYPE marm-meinh,
          END OF itab1.
                            V A R I A B L E S
    Global Variables
    DATA: g_repid   LIKE sy-repid.       "Report ID
    DATA: dy_table TYPE REF TO data,
          dy_line TYPE REF TO data.
                      S E L E C T I O N   S C R E E N
    For User Input(Selection Criteria)
    SELECTION-SCREEN BEGIN OF BLOCK b0 WITH FRAME TITLE text-001.
    SELECT-OPTIONS :
      s_mtart FOR mara-mtart OBLIGATORY,    " Material type
      s_vkorg FOR mvke-vkorg OBLIGATORY,    " Sales Organization
      s_plant FOR marc-werks,               " Plant
      s_meinh FOR marm-meinh.               " Unit of Measure
    SELECTION-SCREEN END OF BLOCK b0.
                         I N I T I A L I Z A T I O N
    INITIALIZATION.
    Program ID
      g_repid = sy-repid.                      " Prog ID
                     A T   S E L E C T I O N -  S C R E E N
    AT SELECTION-SCREEN.
    Validate Material Type
      PERFORM check_mtart.
    Validate Sales Organization
      PERFORM check_vkorg.
    Validate Plant
      PERFORM check_werks.
    Validate Unit of Measure (UOM)
      PERFORM check_meinh.
                      S T A R T - O F  - S E L E C T I O N
    START-OF-SELECTION.
    Get Data
      PERFORM get_data.
    FIELD CATALOG FOR ALV
      PERFORM fill_field_catalog_uom USING tb_uom_cat.
    FIELD CATALOG FOR CREATING DYNAMIC INTERNAL TABLE
      PERFORM fill_field_catalog_dy USING tb_dy_cat.
    CREATING DYNAMIC INTERNAL TABLE
      PERFORM dy_tab_create.
                       End of selection                                  *
                              F O R M S
    *&      Form  CHECK_MTART
          Validate Material Type
    FORM check_mtart .
    Material Type
      DATA :l_mtart LIKE t134-mtart.
      CHECK NOT s_mtart IS INITIAL.
      SELECT mtart UP TO 1 ROWS
        INTO l_mtart
        FROM t134
       WHERE mtart IN s_mtart.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Material Type'(002).
      ENDIF.
    ENDFORM.                    " CHECK_MTART
    *&      Form  CHECK_VKORG
          Validate Sales Organization
    FORM check_vkorg .
    Sales Oraganization
      DATA :l_vkorg LIKE tvko-vkorg.
      CHECK NOT s_vkorg IS INITIAL.
      SELECT vkorg UP TO 1 ROWS
        INTO l_vkorg
        FROM tvko
       WHERE vkorg IN s_vkorg.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Sales Organization'(003).
      ENDIF.
    ENDFORM.                    " CHECK_VKORG
    *&      Form  CHECK_WERKS
          Validate Plant
    FORM check_werks .
    Plant
      DATA :l_werks LIKE t001w-werks.
      CHECK NOT s_plant IS INITIAL.
      SELECT werks UP TO 1 ROWS
        INTO l_werks
        FROM t001w
       WHERE werks IN s_plant.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Plant'(004).
      ENDIF.
    ENDFORM.                    " CHECK_WERKS
    *&      Form  CHECK_MEINH
          Validate Unit of Measure (UOM)
    FORM check_meinh .
    Unit of Measure (UOM)
      DATA :l_meinh LIKE t006-msehi.
      CHECK NOT s_meinh IS INITIAL.
      SELECT msehi UP TO 1 ROWS
        INTO l_meinh
        FROM t006
        WHERE msehi IN s_meinh.
      ENDSELECT.
      IF sy-subrc <> 0.
        MESSAGE e899(mm) WITH 'Invalid Unit of Measure'(005).
      ENDIF.
    ENDFORM.                    " CHECK_MEINH
    *&      Form  FILL_FIELD_CATALOG_UOM
          text
         FIELD CATALOG FOR ALV
    FORM fill_field_catalog_uom  USING l_fieldcat TYPE  slis_t_fieldcat_alv.
      DATA: BEGIN OF l_itab OCCURS 0,
            meinh TYPE marm-meinh,
            END OF l_itab.
    DATA: l_count TYPE i.
      DATA: l_fieldcat_col_pos TYPE i.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'MTART'.
      ls_fieldcat-key         = ''.
      ls_fieldcat-col_pos     = 1.
      ls_fieldcat-ref_fieldname = 'MTART'.
      ls_fieldcat-ref_tabname   = 'MARA'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'VKORG'.
      ls_fieldcat-key         = ''.
      ls_fieldcat-col_pos     = 2.
      ls_fieldcat-ref_fieldname = 'VKORG'.
      ls_fieldcat-ref_tabname   = 'MVKE'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'WERKS'.
      ls_fieldcat-key         = ''.
      ls_fieldcat-col_pos     = 3.
      ls_fieldcat-ref_fieldname = 'WERKS'.
      ls_fieldcat-ref_tabname   = 'MARC'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname   = 'MATNR'.
      ls_fieldcat-key         = 'x'.
      ls_fieldcat-col_pos     = 4.
      ls_fieldcat-ref_fieldname = 'MATNR'.
      ls_fieldcat-ref_tabname   = 'MARA'.
      APPEND ls_fieldcat TO l_fieldcat.
      CLEAR ls_fieldcat.
      l_fieldcat_col_pos = 5.
    Getting the UOM as per user selection
      SELECT  msehi  FROM t006
        INTO TABLE l_itab
        WHERE msehi IN s_meinh.
      LOOP AT l_itab.
        ls_fieldcat-fieldname   = l_itab-meinh.
        ls_fieldcat-key         = ''.
        ls_fieldcat-col_pos     = l_fieldcat_col_pos.
        ls_fieldcat-ref_fieldname = 'MSEHI'.
        ls_fieldcat-ref_tabname   = 'T006'.
        APPEND ls_fieldcat TO l_fieldcat.
        CLEAR ls_fieldcat.
        l_fieldcat_col_pos = l_fieldcat_col_pos + 1.
      ENDLOOP.
    ENDFORM.                    " FILL_FIELD_CATALOG_UOM
    *&      Form  DISPLAY_DATA
          text
    -->  p1        text
    <--  p2        text
    FORM display_data .
    ENDFORM.                    " DISPLAY_DATAENDFORM.                    " FILL_FIELD_CATALOG_UOM
    *&      Form  DY_TAB_CREATE
          text
    -->  p1        text
    <--  p2        text
    FORM dy_tab_create .
    CREATE DYNAMIC INTERNAL TABLE AND ASSIGN TO FS
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog = tb_dy_cat
        IMPORTING
          ep_table        = dy_table.
      ASSIGN dy_table->* TO <dyn_table>.
    *CREATE DYNAMIC WORK AREA AND AASIGN TO FS
      CREATE DATA dy_line LIKE LINE OF  <dyn_table>.
      ASSIGN dy_line->* TO <dyn_wa>.
    ENDFORM.                    " DY_TAB_CREATE
    *&      Form  FILL_FIELD_CATALOG_DY
          text
        FIELD CATALOG FOR CREATING A DYNAMIC TABLE
    FORM fill_field_catalog_dy  USING  l_fieldcat TYPE lvc_t_fcat.
      DATA: BEGIN OF l_itab OCCURS 0,
           meinh TYPE marm-meinh,
           END OF l_itab.
      DATA: l_count TYPE i.
      DATA: l_fieldcat_col_pos TYPE i.
      DATA: l_umren TYPE string.
      DATA: l_var TYPE string.
      CLEAR ls_fieldcat.
      ls_fieldcat1-fieldname   = 'MTART'.
      ls_fieldcat1-key         = ''.
      ls_fieldcat1-col_pos     = 1.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 4.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      ls_fieldcat1-fieldname   = 'VKORG'.
      ls_fieldcat1-key         = ''.
      ls_fieldcat1-col_pos     = 2.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 4.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      ls_fieldcat1-fieldname   = 'WERKS'.
      ls_fieldcat1-key         = ''.
      ls_fieldcat1-col_pos     = 3.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 4.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      ls_fieldcat1-fieldname   = 'MATNR'.
      ls_fieldcat1-key         = 'x'.
      ls_fieldcat1-col_pos     = 4.
      ls_fieldcat1-datatype = 'char'.
      ls_fieldcat1-intlen = 18.
      APPEND ls_fieldcat1 TO l_fieldcat.
      CLEAR ls_fieldcat1.
      l_fieldcat_col_pos = 5.
    Getting the UOM as per user selection
      SELECT  msehi  FROM t006
        INTO TABLE l_itab
        WHERE msehi IN s_meinh.
      SORT l_itab ASCENDING.
      DESCRIBE TABLE l_itab LINES l_count.
      LOOP AT l_itab.
        ls_fieldcat1-fieldname   = l_itab-meinh.
        ls_fieldcat1-key         = ''.
        ls_fieldcat1-col_pos     = l_fieldcat_col_pos.
        ls_fieldcat1-datatype = 'dec'.
        ls_fieldcat1-intlen = 5.
        APPEND ls_fieldcat1 TO l_fieldcat.
        CLEAR ls_fieldcat1.
        l_fieldcat_col_pos = l_fieldcat_col_pos + 1.
      ENDLOOP.
    ENDFORM.                    " FILL_FIELD_CATALOG_DY
    *&      Form  GET_DATA
          text
    -->  p1        text
    <--  p2        text
    FORM get_data .
      SELECT
      amtart bvkorg cwerks amatnr
      INTO TABLE itab
      FROM mara AS a
      JOIN mvke AS b ON  bmatnr = amatnr
      JOIN marc AS c ON  cmatnr = bmatnr
      JOIN marm AS d ON  dmatnr = cmatnr
      WHERE a~mtart IN s_mtart
       AND  b~vkorg IN s_vkorg
       AND  c~werks IN s_plant.
      SORT itab ASCENDING.
      DELETE ADJACENT DUPLICATES FROM itab.
      SELECT matnr umren meinh FROM marm
      APPENDING TABLE itab1
      FOR ALL ENTRIES IN itab
      WHERE matnr = itab-matnr
      AND meinh IN s_meinh .
    ENDFORM.                    " GET_DATA

    can you check this for creation of dynamic table
    ******DATA DECLARATION*****************************
    FIELD-SYMBOLS : <it_final> TYPE STANDARD TABLE,
                    <wa_final> TYPE ANY,
                    <w_field> TYPE ANY.
    ***DYNAMIC CREATION OF FIELDCATALOG****************
    *FIRST 2 FIELDS FIELDS FIELD1 AND FIELD2 ARE CONSTANT, FIELDS OBTAINED IN THE LOOP ENDLOOP ARE DYNAMIC,
    *LIKEWISE DYNAMIC FIELDCATALOG IS CREATED
      wa_fieldcatalog-fieldname  = 'FIELD1'.
      wa_fieldcatalog-ref_table  = 'E070'.
      wa_fieldcatalog-outputlen  = '13'.
      wa_fieldcatalog-reptext    = 'Created On'.
      wa_fieldcatalog-seltext    = 'Created On'.
      APPEND wa_fieldcatalog TO it_fieldcatalog.
      CLEAR wa_fieldcatalog.
      wa_fieldcatalog-fieldname  = 'FIELD1'.
      wa_fieldcatalog-ref_table  = 'E070'.
      wa_fieldcatalog-outputlen  = '13'.
      wa_fieldcatalog-reptext    = 'Created On'.
      wa_fieldcatalog-seltext    = 'Created On'.
      APPEND wa_fieldcatalog TO it_fieldcatalog.
      CLEAR wa_fieldcatalog.
      LOOP AT it_mandt WHERE mandt IN s_mandt.
        CONCATENATE 'CLNT' it_mandt INTO wa_fieldcatalog-fieldname.
        wa_fieldcatalog-inttype    = 'NUMC'.
        wa_fieldcatalog-outputlen  = '14'.
        wa_fieldcatalog-reptext    = it_mandt.
        wa_fieldcatalog-seltext    = it_mandt.
        APPEND wa_fieldcatalog TO it_fieldcatalog.
        CLEAR :wa_fieldcatalog ,it_mandt.
      ENDLOOP.
    ********CREATE DYNAMIC TABLE************************
      CALL METHOD cl_alv_table_create=>create_dynamic_table
        EXPORTING
          it_fieldcatalog           = it_fieldcatalog
        IMPORTING
          ep_table                  = new_table
        EXCEPTIONS
          generate_subpool_dir_full = 1
          OTHERS                    = 2.
      IF sy-subrc <> 0.
    *  MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
      ASSIGN new_table->* TO <it_final>.
    *********CREATE WORK AREA****************************
    CREATE DATA new_line LIKE LINE OF <it_final>.
      ASSIGN new_line->* TO <wa_final>.
    *********INSERTTING WORK AREAR TO INTERNAL TABLE******
        INSERT <wa_final> INTO TABLE <it_final>.
    *******POPULATING DATA******************************* 
      LOOP.
       ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_final> TO <w_field>.
       <w_field> = '12345'.
        ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_final> TO <w_field>.
       <w_field> = '21453DD'.
       FIELD1 AND FIELD2 ARE COMPONENTS OF FIELDCATALOG.
    ENDLOOP.     
      ENDLOOP.

Maybe you are looking for

  • Ipad charger wont charge??

    When my Ipad is on it wont charge but when it is dead it charges. Help!! I have the belkin cable charger and its still in cgood condition

  • Where is the firefox tab?

    I just downloaded firefox 4 and listened to tutuorial. There is no firefox tab at the top left of my browser page as demonstrated in the video. How come? How can I install it to use the demonstrated features?

  • Making a second iTunes Library

    I have a 15 gig itunes library, mixed between mp3 and aac files. I want to make a duplicate library, on my external HD, to make all the files mp3 to make some mp3 cds and dvds with. Is there an easy means to this? I have a current back up on my exter

  • How to print one sales order output from two printers using single command.

    Dear all, Our requirement is to print single sales order from two different printers, where as one printout for sales order output and another for sales order cash receipt. We developed two smartforms for both outputs. But we are struggling to assign

  • I cal duplicating events after Lion install.

    I just installed Lion on my macbook but I seem to be having a problem with ical. Its duplicated all my events but when I try to delete the duplications the whole event disappears. The calendar syncs with gmail but I've never had a problem with duplic