2014 Nov 26 1:05 PM
Hi All,
I have a requirement.
1 Tiger 34
2 Lion 20
3 crow 67
4 Kite 89
I want to add two rows based on condition and merge to one.
When 1 or 2
result Animal 54
when 3 or 4
result Bird 156
Display in table Animal 54
Bird 156
Thanks in advance.
Bikash
2014 Nov 26 1:12 PM
Hi.
Try This:
DATA: BEGIN OF it_table OCCURS 0,
name(20) TYPE c,
category(20) TYPE c,
value TYPE i,
END OF it_table.
DATA: BEGIN OF it_category OCCURS 0,
category(20) TYPE c,
value TYPE i,
END OF it_category.
it_table-name = 'Tiger'
it_table-category = 'Animal'.
it_table-value = 34.
APPEND it_table.
it_table-name = 'Lion'
it_table-category = 'Animal'.
it_table-value = 20.
APPEND it_table.
it_table-name = 'Crow'
it_table-category = 'Bird'.
it_table-value = 67.
APPEND it_table.
it_table-name = 'Kite'
it_table-category = 'Bird'.
it_table-value = 89.
APPEND it_table.
LOOP AT it_table.
MOVE-CORRESPONDING it_table TO it_category.
COLLECT it_category.
ENDLOOP.
If you can't determine category manually, well you should create an array of data containing categories and animals and search in this array every time you loop in your animals table to collect data.
Hope to help
Bye
2014 Nov 26 1:28 PM
Hi Roberto,
I cannot add a field in the Table.
Please provide the solution without collect statement too.
Regards,
Bikash.
2014 Nov 26 1:37 PM
well ok. You have to make a link between "Lion" and "Animal". How do you manage this? You could use a condition like IF IN ("LION","TIGER","EAGLE",etc) then "ANIMAL"
And you don't need to add field.
For the collect problem you have to simulate the solution that's means use a variable to remember that a Tiger is an Animal and sum this value to the Animal category.
Without your code functional specifications it's hard to help with a complete solution.
Hope to help
Bye
2014 Nov 26 1:41 PM
I understand this:
LOOP AT your_table.
IF tiger IN (tiger,eagle,cow,etc) THEN
ADD 1 TO your_table_row-count_animals
your_table_row-1col = 'Animal'.
MODIFY your_table
ELSEIF tiger IN (crow, kite, muccalapa, etc)
ADD 1 TO your_table_row-count_birds
your_table_row-1col = 'Birds'.
MODIFY your_table.
ELSE..etc..
ENDIF.
ENDLOOP.
Whenever you'll modify a row in your table you'll have your collected data..
Hope to help.
Bye
2014 Nov 26 1:31 PM
Hi,
You can use the COLLECT statement.
Example:
Declare 2 structures:
------------------------------
ls_animal
ls_bird
ITAB layout:
------------------
Col1 (Char) - Col2 (Char) - Col3(Num)
CODE
-----------
Loop at itab.
case itab-col1
when '1' or '2'
ls_animal-col1 = ' '
ls_animal-col2 = 'animal'
ls_animal-col3 = itab-col3
COLLECT ls_animal into table itab.
when '3' or '4'
ls_bird-col1 = ' '
ls_bird-col2 = 'bird'
ls_bird-col3 = itab-col3
COLLECT ls_bird into table itab.
endcase.
EndLoop.
You can also have a look at the following topic
http://scn.sap.com/thread/538968
plz reward points if it helps
Regards
Bert
2014 Nov 26 2:08 PM
hi bikash,
I have added one more field in the first internal table.
in second internal table I am getting the result as per your requirement.
kindly have a look at the code:
TYPES: begin of ty,
a type i,
b(10) type c,
c type i,
d(10) type c,
end of ty.
TYPES: begin of ty1,
a(10) type c,
b type i,
end of ty1.
DATA: WA type ty,
WA1 type ty1,
itab type STANDARD TABLE OF ty,
itab1 type STANDARD TABLE OF ty1.
wa-a = 1.
wa-b = 'Tiger'.
wa-c = 34.
wa-d = 'Animal'.
APPEND wa to itab.
wa-a = 2.
wa-b = 'Lion'.
wa-c = 20.
wa-d = 'Animal'.
APPEND wa to itab.
wa-a = 3.
wa-b = 'Crow'.
wa-c = 67.
wa-d = 'Bird'.
APPEND wa to itab.
wa-a = 4.
wa-b = 'Kite'.
wa-c = 89.
wa-d = 'Bird'.
APPEND wa to itab.
LOOP AT itab into wa.
WRITE: / wa-a, wa-b, wa-c.
wa1-a = wa-d.
wa1-b = wa-c.
collect wa1 into itab1.
ENDLOOP.
write: / '-------------'.
LOOP AT itab1 into wa1.
WRITE: / wa1-a, wa1-b.
ENDLOOP.
2014 Nov 26 2:21 PM
Hi Abdul,
I have already mentioned that extra field cannot be used and collect statement too.
But your approach is correct.
We can modify the text in LOOP.
LOOP.
IF field1 = 1 or field1 = 2.
field2 = 'Animal'.
pass to another work area
collect wa to itab.
else.
pass wa to another wa.
append to itab.
ENDLOOP.
Thanks,
Bikash.
2014 Nov 26 2:29 PM
Hi Bikash,
Can you maintain two separate structures, ls_animal (tiger, lion) and ls_bird (crow, kite).
This shall stand as two separate tables for us.
Then you can LOOP them as previous friends mentioned in here. COLLECT can also be used.
Cheers,
Vivek Raj Kumar M.
2014 Nov 26 2:31 PM
Hi Bikash,
In case, you want any help with COLLECT keyword, here is the ABAP Help Documentation.
Similar to F1 key press on the keyword from ABAP editor.
ABAP Keyword Documentation_COLLECT.
Cheers,
Vivek Raj Kumar M.