Change table row by index - abap

How can I give a field a value with a specific index.
Or if the field is not initial I wanna change the value on this index
Example:
TYPES : BEGIN OF itab,
number1 type n,
endof itab.
lv_tabix = sy-tabix.
itab-number1 index lv-tabix = '1'.

Just use table expressions for this, if you are on the recent ABAP releases:
TRY.
itab[ lv-tabix ]-number1 = '1'.
CATCH cx_sy_itab_line_not_found INTO DATA(exc).
cl_demo_output=>display( exc->get_text( ) ).
ENDTRY.

You should read table by index and assign target line to filed symbol first:
READ TABLE {your itab} INDEX lv_tabix ASSIGNING FIELD-SYMBOL(<fs>).
<fs>-number1 = '1'.
Depending on your server version, seperate field symbol declaration may be needed:
FIELD-SYMBOLS <fs> TYPE {your table line type}
Check READ TABLE online help https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abapread_table_outdesc.htm#!ABAP_ALTERNATIVE_2#2#

Check the help of the sentence MODIFY (itab):
https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abapmodify_itab_single.htm
DATA: workarea TYPE your_type.
workarea-field = value.
"modifying for an index
MODIFY your_table INDEX your_index FROM workarea TRANSPORTING field.
"modifying for a condition
MODIFY your_table FROM workarea TRANSPORTING field WHERE your_condition.
Or you can use FIELD-SYMBOLS, as has been pointed before me by Anton.

This?
TYPES: begin OF ty_itab,
number1 TYPE n,
END OF ty_itab.
DATA: itab TYPE TABLE OF ty_itab,
wa_itab like line of itab,
lv_tabix TYPE sy-tabix.
APPEND wa_itab TO itab.
lv_tabix = sy-tabix.
READ TABLE itab INTO wa_itab INDEX lv_tabix.
wa_itab-number1 = '1'.
MODIFY itab FROM wa_itab INDEX lv_tabix.
BREAK-POINT.

Related

Looping through a dynamic internal table

I am trying to split internal table to smaller chunks.
In below example the internal table big_table is split into small tables.
My question is how do we get the actual data from lt_small_tables for further processing.
TYPES: BEGIN OF lty_line,
column1 TYPE i,
column2 TYPE c LENGTH 4,
END OF lty_line.
CONSTANTS: lc_test_data_amount TYPE i VALUE 44,
lc_split_at_amount TYPE i VALUE 7,
DATA: lt_big_table TYPE STANDARD TABLE OF lty_line,
lv_string TYPE string,
lt_small_tables TYPE STANDARD TABLE OF REF TO data,
lr_small_table TYPE REF TO data.
FIELD-SYMBOLS: <lg_target> TYPE STANDARD TABLE.
" Generate test data
DO lc_test_data_amount TIMES.
CALL FUNCTION 'GENERAL_GET_RANDOM_STRING'
EXPORTING number_chars = 4 " Specifies the number of generated chars
IMPORTING random_string = lv_string. " Generated string
APPEND VALUE #( column1 = sy-index
column2 = CONV #( lv_string ) ) TO lt_big_table.
ENDDO.
" Split
DATA(lo_descr) = CAST cl_abap_tabledescr(
cl_abap_typedescr=>describe_by_data( lt_big_table ) ).
LOOP AT lt_big_table ASSIGNING FIELD-SYMBOL(<ls_line>).
IF ( sy-tabix - 1 ) MOD lc_split_at_amount = 0.
CREATE DATA lr_small_table TYPE HANDLE lo_descr.
ASSERT lr_small_table IS BOUND.
APPEND lr_small_table TO lt_small_tables.
ASSIGN lr_small_table->* TO <lg_target>.
ASSERT <lg_target> IS ASSIGNED.
ENDIF.
APPEND <ls_line> TO <lg_target>.
ENDLOOP.
Thanks,
Kris
There are at least a couple of ways
Solution 1: cast internal table reference into a known type, so that you can directly access its fields.
FIELD-SYMBOLS: <fs_table> like lt_big_table.
LOOP AT lt_small_tables into lr_small_table.
ASSIGN lr_small_table->* TO <fs_table>.
ASSERT <fs_table> IS ASSIGNED.
LOOP AT <fs_table> into data(ls_line).
" do something with line
write: / ls_line-column1, ls_line-column2.
ENDLOOP.
ENDLOOP.
Solution 2: dynamic access to the tables fields
FIELD-SYMBOLS: <generic_fs_table> TYPE ANY TABLE.
LOOP AT lt_small_tables into lr_small_table.
ASSIGN lr_small_table->* TO <generic_fs_table>.
ASSERT sy-subrc = 0.
LOOP AT <generic_fs_table> ASSIGNING FIELD-SYMBOL(<generic_fs_line>).
" dinamically access to line fields
ASSIGN COMPONENT 'COLUMN1' of STRUCTURE <generic_fs_line> to FIELD-SYMBOL(<generic_fs_field1>).
ASSERT sy-subrc = 0.
ASSIGN COMPONENT 'COLUMN2' of STRUCTURE <generic_fs_line> to FIELD-SYMBOL(<generic_fs_field2>).
ASSERT sy-subrc = 0.
" do something with component2
write: / <generic_fs_field1>, <generic_fs_field2>.
ENDLOOP.
ENDLOOP.

Counting Duplicate Row in internal table

I need to count the number of duplicate rows in an internal table base on one field.
I have tried to create a work area and counting the duplicate data but the problem is it counts all duplicate data. My purpose is to count duplicate data by the same date.
DATA: gv_line TYPE i.
gv_line = 0.
LOOP AT i_sect_proe.
IF wa_sect_proe IS INITIAL.
wa_sect_proe = i_sect_proe.
CONTINUE.
ENDIF.
IF wa_sect_proe-/smr/wondat EQ i_final_f-/smr/wondat.
gv_line = gv_line + 1.
ENDIF.
i_sect_proe-/smr/line = gv_line.
ENDLOOP.
The code I've tried displays the number off all duplicate data.
DATA: BEGIN OF lt_result OCCURS 0,               date TYPE datum,               count TYPE i,           END OF lt_result.
SORT yourTable BY dateField.
LOOP AT yourTable.
    lt_result-date   = yourTable-dateField.
    lt_result-count = 1.
    COLLECT lt_result INTO lt_result.
ENDLOOP.
Result in lt_result[].

Creating a range for a field from internal table using RTTS

I want to create a function/custom class method that takes in 2 parameters:
1) IM_ITAB type ANY TABLE
2) IM_COMPONENT type STRING
and returns 1 parameter:
1) EX_RANGE type PIQ_SELOPT_T
So, algorithm is like this:
First of all, we check if the column with a component name at all exists
Then, we check that internal table is not empty.
Then, we loop through internal table assigning component and filling range table. Code is below.
METHODS compose_range_from_itab
IMPORTING
IM_ITAB type ANY TABLE
IM_COMPONENT type STRING
EXPORTING
EX_RANGE type PIQ_SELOPT_T.
...
METHOD compose_range_from_itab.
DATA: lo_obj TYPE REF TO cl_abap_tabledescr,
wa_range TYPE selopt,
lt_range TYPE piq_selopt_t.
FIELD-SYMBOLS: <fs_line> TYPE ANY,
<fs_component> TYPE ANY.
lo_obj ?= cl_abap_typedescr=>describe_by_data( p_data = im_itab ).
READ TABLE lo_obj->key TRANSPORTING NO FIELDS WITH KEY name = im_component.
IF sy-subrc IS INITIAL.
IF LINES( im_itab ) GT 0.
LOOP AT im_itab ASSIGNING <fs_line>.
ASSIGN COMPONENT im_component OF STRUCTURE <fs_line> TO <fs_component>.
wa_range-sign = 'I'.
wa_range-option = 'EQ'.
wa_range-low = <fs_component>.
APPEND wa_range TO lt_range.
ENDLOOP.
SORT lt_range BY low.
DELETE ADJACENT DUPLICATES FROM lt_range COMPARING low.
ex_range[] = lt_range[].
ENDIF.
ENDIF.
ENDMETHOD.
But I want to improve the method further. If the imported internal table has, let's say, 255 columns, then it will take longer to loop through such table. But I need only one column to compose the range.
So I want to get components of internal table, then choose only one component, create a new line type containing only that component, then create internal table with that line type and copy.
Here is the pseudo code corresponding to what I want to achieve:
append corresponding fields of im_itab into new_line_type_internal_table.
How can I "cut out" one component and create a new line type using RTTS?
You are overcomplicating everything, you don't need RTTS for that.
DEFINE make_range.
ex_range = VALUE #( BASE ex_range ( sign = 'I' option = 'EQ' low = &1 ) ).
END-OF-DEFINITION.
LOOP AT im_itab ASSIGNING FIELD-SYMBOL(<fs_line>).
ASSIGN COMPONENT im_component OF STRUCTURE <fs_line> TO FIELD-SYMBOL(<fs_field>).
CHECK sy-subrc = 0 AND <fs_field> IS NOT INITIAL.
make_range <fs_field>.
ENDLOOP.
And yes, as Sandra said, you won't gain any performance with RTTS, just the opposite.
Surprisingly, this variant turned out to be faster:
CLASS-METHODS make_range_variant_2
IMPORTING
sample TYPE table_type
column TYPE string
RETURNING
VALUE(result) TYPE range_type.
METHOD make_range_variant_2.
TYPES:
BEGIN OF narrow_structure_type,
content TYPE char32,
END OF narrow_structure_type.
TYPES narrow_table_type TYPE STANDARD TABLE OF narrow_structure_type WITH EMPTY KEY.
DATA narrow_table TYPE narrow_table_type.
DATA(mapping) =
VALUE cl_abap_corresponding=>mapping_table_value(
( kind = cl_abap_corresponding=>mapping_component srcname = column dstname = 'CONTENT' ) ).
DATA(mover) =
cl_abap_corresponding=>create_with_value(
source = sample
destination = narrow_table
mapping = mapping ).
mover->execute(
EXPORTING
source = sample
CHANGING
destination = narrow_table ).
LOOP AT narrow_table ASSIGNING FIELD-SYMBOL(<row>).
INSERT VALUE #(
sign = 'I'
option = 'EQ'
low = <row>-content )
INTO TABLE result.
ENDLOOP.
ENDMETHOD.
CL_ABAP_CORRESPONDING delegates to a kernel function for the structure-to-structure move, which apparently is faster than the ABAP-native ASSIGN COMPONENT [...] OF STRUCTURE [...] TO FIELD-SYMBOL [...]. The actual loop then seems to be faster because it uses fixed-name assignments.
Maybe somebody could verify.
I would not go for a Macro.
Data:
lr_data type ref to data.
FIELD-SYMBOLS:
<lv_component> TYPE any,
<ls_data> TYPE any.
CREATE DATA lr_data LIKE LINE OF im_itab.
ASSIGN lr_data->* TO <ls_data>.
"Check whether im_component exists
ASSIGN COMPONENT im_component OF STRUCTURE <ls_data> TO <lv_component>.
CHECK sy-subrc EQ 0.
LOOP AT im_itab INTO <ls_data>.
APPEND VALUE #( sign = 'I' option = 'EQ' low = <lv_component> ) TO ex_range.
ENDLOOP.

how can I get values from the table

I concatenated values of select-options and a parameter. The condition of that query is based on the concatenated data. I can get all the data i need.
here's my code:
TABLES: bkpf.
SELECT-OPTIONS: s_belnr FOR bkpf-belnr NO-EXTENSION OBLIGATORY .
PARAMETERS: p_ghjahr LIKE bkpf-gjahr DEFAULT sy-datum(4) OBLIGATORY. "Fiscal
DATA: it_con TYPE TABLE OF BKPF,
ls_con TYPE bkpf-AWKEY,
lv_belnr LIKE bkpf-belnr,
IT TYPE STANDARD TABLE OF BKPF,
WA TYPE BKPF.
IF s_belnr-high IS INITIAL.
CONCATENATE s_belnr-low p_ghjahr INTO ls_con.
APPEND ls_con TO it_con.
ELSE.
lv_belnr = s_belnr-low.
WHILE lv_belnr LE s_belnr-high.
CONCATENATE lv_belnr p_ghjahr INTO ls_con.
APPEND ls_con TO it_con.
ADD 1 TO lv_belnr.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lv_belnr
IMPORTING
output = lv_belnr.
ENDWHILE.
ENDIF.
LOOP AT it_concats INTO ls_concats.
SELECT BELNR
FROM BKPF
INTO CORRESPONDING FIELDS OF TABLE IT
FOR ALL ENTRIES IN IT_CONCATS
WHERE AWKEY EQ IT_CONCATS-AWKEY.
ENDLOOP.
LOOP AT IT INTO WA.
WRITE: / WA-BELNR.
ENDLOOP.
Ignoring (because your question is too vague), the type of document you are looking for, I'll suggest something like
(WARNING, I do NOT provide full answers, just code snipets who you must tune to make it work; if someone wants to improve my answer, feel free to do it, and I'll gladly will vote the new one as the good one... if it is)
data: awkey_range type range of bkpf-awkey,
awkey_line like line of awkey_range.
* Fill the awkey_range with something like
awkey_line-sign = 'I'.
awkey_line-option = 'EQ'.
* loop at bkpf_table into bkpf_line.
* concatenate bkpf_line-belnr bkpf_line-ghjahr into awkey_line-low.
* append awkey_line to awkey_range.
* endloop.
* And then a single SQL
select *
from bkpf
into table IT "Ouch, what a name
where awkey in awkey_range.
And it should work, if I'm not missing something.

Type conflict within DELETE itab1 FROM itab2?

I've had a dump recently,
DATA: gt_data TYPE SORTED TABLE OF ty_data WITH NON-UNIQUE KEY bukrs gaapnm,
...
lt_tabdel TYPE standard TABLE OF ty_data.
LOOP AT gt_data ASSIGNING <gf_data>.
IF <gf_data>-KANSW + <gf_data>-KAUFW = 0.
APPEND <gf_data> TO lt_tabdel.
ENDIF.
ENDLOOP.
IF lt_tabdel IS NOT INITIAL.
DELETE gt_data FROM lt_tabdel.
ENDIF.
And on the line with deleting table from internal table - i've had a dump: In statement
Convert object to integer
only numerical type data objects are supported at argument position
"object".
In the present case, operand "object" has the non-numerical data type "TABLE
OF TY_DATA". I just can't understand - why? Both of it had the same type... So, it will be great if you could provide some advice and a bit of explanation of error origins.
You have (inadvertently) used this variant of the DELETE statement that uses FROM and TO to specify indexes, i. e. numbers of table lines. In a sense, you are coding delete all lines in gt_data below the one identified by the line number in lt_tabdel, and the system goes belly-up when trying to convert the contents of lt_tabdel to an integer.
As far as I can see - i. E. if you've provided a complete code sample - this should be sufficient:
LOOP AT gt_data ASSIGNING <gf_data>.
IF <gf_data>-KANSW + <gf_data>-KAUFW = 0.
DELETE gt_data.
CONTINUE. " safety measure
ENDIF.
ENDLOOP.
For an explanation of the CONTINUE statement, check this answer.
IF lt_tabdel IS NOT INITIAL.
DELETE gt_data FROM lt_tabdel.
ENDIF.
* IF lt_tabdel IS NOT INITIAL.
DELETE TABLE gt_data FROM lt_tabdel.
ENDIF. *
Adding TABLE will help you.
Ok, i found solution. Delete - was the wrong command. So i used this one instead:
LOOP AT gt_data ASSIGNING <gf_data>.
IF <gf_data>-KANSW + <gf_data>-KAUFW <> 0.
append <gf_data> to lt_data.
ENDIF.
ENDLOOP.
gt_data[] = lt_data[].
Just filled another table and assigned it contents to the main table.