‎2011 Feb 20 6:38 PM
Halo Experts,
1 I have got 10 attributes like my_attr1-my_attr10
in my class which are type ref to data
Depending upon the reqment I will instatnaite the attributes
In our example I am instantiating 3 attributes.say my_attr1- my_attr3.
my_ref_data( which is the final output) which contains fields from all the three.
This I am able to do.
2 Now I have an itab which has values like
name value
attr1 10
attr2 20
attr2 30
attr3 40
attr3 40
attr3 40
I loop at this itab I have to move this values to seperate attributes like
if name = 'ATTR1' then it should get appended to my_attr1.
if name = 'ATTR2' then it should get appended to my_attr2.
like wise So my_attr1 will have 1 entry and my_attr2 will have two entry and
my_attr3 will have 3 entries.
Can I do this dynamically . Because here we need to generate field symbols dynamically to assign.
otherwise the field symbols values will get overwrittten.
3
Now i should loop thru the attribute my_Attr3 which is having max no of entries
assign my_attr3->* <ft_max_ref_data>.
loop at <ft_max_ref_data> assigning <fs_max_ref_data>.
move-corresponding <fs_max_ref_data> to <fs_ref_data>.
append <fs_ref_data> to <ft_ref_data>.
endloop.
how can i access filled up my attriutes without creating separate field symbols?
Regards
Kallu
‎2011 Feb 20 7:49 PM
2 Now I have an itab which has values like
name value
attr1 10
attr2 20
attr2 30
attr3 40
attr3 40
attr3 40
I loop at this itab I have to move this values to seperate attributes like
if name = 'ATTR1' then it should get appended to my_attr1.
if name = 'ATTR2' then it should get appended to my_attr2.
like wise So my_attr1 will have 1 entry and my_attr2 will have two entry and
my_attr3 will have 3 entries.
Can I do this dynamically . Because here we need to generate field symbols dynamically to assign.
otherwise the field symbols values will get overwrittten.
Like this?
"inside method
field-symbols: <attr> type standard table,
<wa_attr> type any.
data l_attr_name type string.
loop at itab into wa_itab.
concatenate 'MY_' wa_itab-name into l_attr_name. "MY_ATTR1, MY_ATTR2 ...
assign (l_attr_name) to <attr>.
append initial line to <attr> assigning <wa_attr>. "line of MY_ATTR1, MY_ATTR2 ...
move-corresponding wa_itab to <wa_attr>.
endloop.
3
Now i should loop thru the attribute my_Attr3 which is having max no of entries
assign my_attr3->* <ft_max_ref_data>.
loop at <ft_max_ref_data> assigning <fs_max_ref_data>.
move-corresponding <fs_max_ref_data> to <fs_ref_data>.
append <fs_ref_data> to <ft_ref_data>.
endloop.
how can i access filled up my attriutes without creating separate field symbols?
I am affraid you can't. Moving selected data b/w different structured tables requires these tables and work areas for them. As you work with data generically, the only difference is that you must have two field-symbol work areas.
Regards
Marcin
‎2011 Feb 20 7:49 PM
2 Now I have an itab which has values like
name value
attr1 10
attr2 20
attr2 30
attr3 40
attr3 40
attr3 40
I loop at this itab I have to move this values to seperate attributes like
if name = 'ATTR1' then it should get appended to my_attr1.
if name = 'ATTR2' then it should get appended to my_attr2.
like wise So my_attr1 will have 1 entry and my_attr2 will have two entry and
my_attr3 will have 3 entries.
Can I do this dynamically . Because here we need to generate field symbols dynamically to assign.
otherwise the field symbols values will get overwrittten.
Like this?
"inside method
field-symbols: <attr> type standard table,
<wa_attr> type any.
data l_attr_name type string.
loop at itab into wa_itab.
concatenate 'MY_' wa_itab-name into l_attr_name. "MY_ATTR1, MY_ATTR2 ...
assign (l_attr_name) to <attr>.
append initial line to <attr> assigning <wa_attr>. "line of MY_ATTR1, MY_ATTR2 ...
move-corresponding wa_itab to <wa_attr>.
endloop.
3
Now i should loop thru the attribute my_Attr3 which is having max no of entries
assign my_attr3->* <ft_max_ref_data>.
loop at <ft_max_ref_data> assigning <fs_max_ref_data>.
move-corresponding <fs_max_ref_data> to <fs_ref_data>.
append <fs_ref_data> to <ft_ref_data>.
endloop.
how can i access filled up my attriutes without creating separate field symbols?
I am affraid you can't. Moving selected data b/w different structured tables requires these tables and work areas for them. As you work with data generically, the only difference is that you must have two field-symbol work areas.
Regards
Marcin
‎2011 Feb 21 8:57 AM
Halo Marcin,
Thanks for your reply . But I am facing a problem in step 1 while dynamically instantiatinf MY_ATTR1 - MY_ATTR10.
LOOP AT lt_attributes INTO l_attribute.
CONCATENATE 'MY_' l_attribute-name into l_data_ref_name ." attr_1-> my_attr_1
l_table_descr = me->get_single_table_descr( i_attr_name = l_attribute-name ).
CREATE DATA (l_data_ref_name) TYPE handle l_table_descr.
APPEND l_data_ref_name TO l_attr_count-attribute.
ENDLOOP.
But here the create data statement is showing syntiax error . Seems like create data statemnt does not accept dynamic tokens.
Regards
Kallu
‎2011 Feb 21 10:23 AM
Hello,
How is l_table_descr defined? If you may, post the syntax error text?
BR,
Suhas
‎2011 Feb 21 10:34 AM
Hello Suha,
l_table_descr TYPE REF TO cl_abap_tabledescr.
CREATE DATA (l_data_ref_attr_name) type handle l_table_descr.
It is showing syntax error like
field (l_data_ref_attr_name) is unknown.
Regard
Kallu
‎2011 Feb 21 4:52 PM
>
> Halo Marcin,
>
> Thanks for your reply . But I am facing a problem in step 1 while dynamically instantiatinf MY_ATTR1 - MY_ATTR10.
>
>
> LOOP AT lt_attributes INTO l_attribute. > CONCATENATE 'MY_' l_attribute-name into l_data_ref_name ." attr_1-> my_attr_1 > l_table_descr = me->get_single_table_descr( i_attr_name = l_attribute-name ). > CREATE DATA (l_data_ref_name) TYPE handle l_table_descr. > APPEND l_data_ref_name TO l_attr_count-attribute. > ENDLOOP. >
>
> But here the create data statement is showing syntiax error . Seems like create data statemnt does not accept dynamic tokens.
>
> Regards
> Kallu
I think I slowly see your goal. Is it something like?
LOOP AT lt_attributes INTO l_attribute.
"here you get name of attribute which is a data reference itself
CONCATENATE 'MY_' l_attribute-name into l_data_ref_name ."my_attr1, my_attr2 ...
"here you need to assign this data reference (not the value behind it)
assing (l_data_ref_name) into <ref>.
"now <ref> holds value of my_attr1, my_attr2
"(field symbol value is nothing but a data reference)
"here you create dynamic type
l_table_descr = me->get_single_table_descr( i_attr_name = l_attribute-name ).
create data <ref> type handle l_table_descr.
"now <ref> is data reference of type l_table_descr
APPEND l_data_ref_name TO l_attr_count-attribute.
ENDLOOP.
Check this out if that will work.
@Suhas
"Use the dereferencing operator '->*' while building the dynamic token
CONCATENATE 'MY_' wa-name '->*' INTO v_dref_name. CONDENSE v_dref_name.
I think that would work if we wanted to get the value behind the attribute (data reference). Apparently OP meant something different, but the explanation came after your post.
Sorry guys if these suggestions are too superficial but I am just ahead my holiday;)
Regards
Marcin
‎2011 Feb 21 5:43 AM
Hello,
A small modification to our friend Marcin's response. Since you are trying to ASSIGN a data reference variable to a field-symbol(SAP terminology for this is 'dereferencing' a data reference variable), you need to use the dereferencing operator '->*'.
Something like this:
"Use the dereferencing operator '->*' while building the dynamic token
CONCATENATE 'MY_' wa-name '->*' INTO v_dref_name. CONDENSE v_dref_name. I've modified Marcin's code a lil' bit to avoid dereferencing the data ref. variable for every loop pass(since you've multiple lines for a single attribute).
wa-name = 'ATTR1'. wa-value = '101'. APPEND wa TO itab.
wa-name = 'ATTR2'. wa-value = '201'. APPEND wa TO itab.
wa-name = 'ATTR2'. wa-value = '202'. APPEND wa TO itab.
wa-name = 'ATTR3'. wa-value = '301'. APPEND wa TO itab.
wa-name = 'ATTR3'. wa-value = '302'. APPEND wa TO itab.
wa-name = 'ATTR3'. wa-value = '303'. APPEND wa TO itab.
SORT itab BY name value.
* Create the data object from the data ref. attr.
* For sake of simplicity i've used SELOPT, you can use your structure
CREATE DATA:
my_attr1 TYPE STANDARD TABLE OF selopt,
my_attr2 TYPE STANDARD TABLE OF selopt,
my_attr3 TYPE STANDARD TABLE OF selopt.
LOOP AT itab INTO wa.
* Dereference the data refer. attribute
AT NEW name.
CONCATENATE 'MY_' wa-name '->*' INTO v_dref_name. CONDENSE v_dref_name.
ASSIGN (v_dref_name) TO <itab>.
ENDAT.
UNASSIGN <wa>.
CHECK <itab> IS ASSIGNED.
APPEND INITIAL LINE TO <itab> ASSIGNING <wa>.
ASSIGN COMPONENT 'SIGN' OF STRUCTURE <wa> TO <val>. <val> = 'I'.
ASSIGN COMPONENT 'OPTION' OF STRUCTURE <wa> TO <val>. <val> = 'EQ'.
ASSIGN COMPONENT 'LOW' OF STRUCTURE <wa> TO <val>. <val> = wa-value.
ENDLOOP.BR,
Suhas
‎2011 Feb 21 5:50 PM
What you need to do is something like this. I have created a table T_ATTR to store the attributes rather than MY_ATTR1 to MY_ATTR10.
*
CLASS lcl_dyna DEFINITION.
PUBLIC SECTION.
TYPES:
BEGIN OF lty_attr,
no TYPE i,
attr TYPE REF TO data,
END OF lty_attr.
DATA: t_attr TYPE STANDARD TABLE OF lty_attr.
TYPES:
BEGIN OF lty_data,
name TYPE char30,
value TYPE i,
END OF lty_data,
ty_t_data TYPE STANDARD TABLE OF lty_data.
METHODS:
set_attr IMPORTING itab TYPE ty_t_data.
ENDCLASS. "lcl_dyna DEFINITION
START-OF-SELECTION.
DATA: o_dyna TYPE REF TO lcl_dyna.
DATA: itab TYPE lcl_dyna=>ty_t_data.
DATA: lwa_data LIKE LINE OF itab.
lwa_data-name = 'attr1'. lwa_data-value = '10'. APPEND lwa_data TO itab.
lwa_data-name = 'attr2'. lwa_data-value = '20'. APPEND lwa_data TO itab.
lwa_data-name = 'attr2'. lwa_data-value = '21'. APPEND lwa_data TO itab.
lwa_data-name = 'attr3'. lwa_data-value = '30'. APPEND lwa_data TO itab.
lwa_data-name = 'attr3'. lwa_data-value = '31'. APPEND lwa_data TO itab.
lwa_data-name = 'attr3'. lwa_data-value = '32'. APPEND lwa_data TO itab.
CREATE OBJECT o_dyna.
o_dyna->set_attr( itab ).
*
CLASS lcl_dyna IMPLEMENTATION.
METHOD set_attr.
DATA: lwa_data LIKE LINE OF itab.
FIELD-SYMBOLS: <wa_attr> LIKE LINE OF t_attr.
DATA: lv_no TYPE i.
* Dynamic Table creation
DATA: lo_struct TYPE REF TO cl_abap_structdescr,
lo_element TYPE REF TO cl_abap_elemdescr,
lo_new_type TYPE REF TO cl_abap_structdescr,
lo_new_tab TYPE REF TO cl_abap_tabledescr,
lo_data TYPE REF TO data,
lt_comp TYPE cl_abap_structdescr=>component_table,
lt_tot_comp TYPE cl_abap_structdescr=>component_table,
la_comp LIKE LINE OF lt_comp.
* field symbols to access the dynamic table
FIELD-SYMBOLS: <f_tab> TYPE STANDARD TABLE,
<f_line> TYPE ANY,
<f_field> TYPE ANY.
IF lo_new_tab IS INITIAL.
* Element Description
lo_element ?= cl_abap_elemdescr=>describe_by_name( 'INT4' ).
* Field name
la_comp-name = 'ATTR_VALUE'.
* Field type
la_comp-type = cl_abap_elemdescr=>get_i( ).
* Filling the component table
APPEND la_comp TO lt_tot_comp.
* 3. Create a New Type
lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).
* 4. New Table type
lo_new_tab = cl_abap_tabledescr=>create(
p_line_type = lo_new_type
p_table_kind = cl_abap_tabledescr=>tablekind_std
p_unique = abap_false ).
ENDIF.
LOOP AT itab INTO lwa_data.
lv_no = lwa_data-name+4(1).
READ TABLE t_attr ASSIGNING <wa_attr> WITH KEY no = lv_no.
IF sy-subrc NE 0.
APPEND INITIAL LINE TO t_attr ASSIGNING <wa_attr>.
<wa_attr>-no = lv_no.
* 5. data to handle the new table type
CREATE DATA <wa_attr>-attr TYPE HANDLE lo_new_tab.
ENDIF.
ASSIGN <wa_attr>-attr->* TO <f_tab>.
CREATE DATA lo_data TYPE HANDLE lo_new_type.
ASSIGN lo_data->* TO <f_line>.
ASSIGN COMPONENT 'ATTR_VALUE' OF STRUCTURE <f_line> TO <f_field>.
<f_field> = lwa_data-value.
APPEND <f_line> TO <f_tab>.
ENDLOOP.
* Now all values are added to T_ATTR table
* after certain logic, we know that max is Attr 3
READ TABLE t_attr ASSIGNING <wa_attr> WITH KEY no = 3.
IF sy-subrc EQ 0.
ASSIGN <wa_attr>-attr->* TO <f_tab>.
LOOP AT <f_tab> ASSIGNING <f_line>.
ASSIGN COMPONENT 'ATTR_VALUE' OF STRUCTURE <f_line> TO <f_field>.
WRITE: / <f_field>.
ENDLOOP.
ENDIF.
ENDMETHOD. "set_attr
ENDCLASS. "lcl_dyna IMPLEMENTATION
Regards,
Naimesh Patel