how to code my requirement?

Question:
Hi guys,
I am facing a problem with logic. I have an input file containing line items.
the first field is parent and second is child.
i can have multiple childs for each parent and multiple parents in a single file.
eg: p1 c1
p1 c2
p1 c3
p2 c4
p2 c5
p3 c6
p3 c7 so on.
i want to break the file into two files,
one containg the parent and a number telling the children it has
like
p1 3
p2 2
p3 2
and another containing the children with key field as parent (basically the initial file)
how can i code this requirement? it is urgent
Any help is appreciated, thanks in advance.
Answer:
I guess you can just load the file in an internal table with a header line (let's call it TABLE1) with a field PARENT and a field CHILD, and create a second table with a header line (let's call it TABLE2) with the field PARENT and the field NUMBER. Then you do a LOOP on TABLE1 and ADD 1 to the field TABLE2-NUMBER on every loop pass. Then you do an AT END OF PARENT statement in which you move the field TABLE1-PARENT to the field TABLE2-PARENT, APPEND TABLE2 and initialize the header line of TABLE2.
After that you write a file with the first table and a file with the second.
That's pretty much it, if I understand your question correctly.
Hope this helps.
Answer:
Hi,
thanks for the reply.
i am curious instead of using the at end of, if we can use collect.
Thanks
Answer:
what will collect do?
data : begin of itab occurs 10,
f1(2),
f2(2),
end of itab.
itab-f1 = 'p1'.
itab-f2 = 'c1'.
append itab.
itab-f2 = 'c2'.
append itab.
itab-f2 = 'c3'.
append itab.
itab-f2 = 'c4'.
append itab.
itab-f1 = 'p2'.
itab-f2 = 'c1'.
append itab.
itab-f2 = 'c2'.
append itab.
itab-f2 = 'c3'.
append itab.
itab-f2 = 'c4'.
append itab.
itab-f1 = 'p3'.
itab-f2 = 'c1'.
append itab.
itab-f2 = 'c2'.
append itab.
itab-f2 = 'c3'.
append itab.
itab-f2 = 'c4'.
append itab.
itab-f1 = 'p4'.
itab-f2 = 'c2'.
append itab.
itab-f2 = 'c3'.
append itab.
itab-f2 = 'c4'.
append itab.
data :counter type i ,counter1 type i value 1.
loop at itab.
at end of f1.
counter1 = sy-tabix - counter.
counter = sy-tabix.
write :/ counter1,itab-f1.
endat.
endloop.
_________________
regards
venkat
Answer:
venkat,
i understood your logic, but curious if we need an initial value of 1 to counter1.
Answer:
no we donot need it
i copied it from some other place
_________________
regards
venkat
Answer:
parent_child,
I agree, using COLLECT instead of AT END OF would be an enven better solution.
table2-counter = 1.
LOOP AT table1.
  table2-parent = table1-parent.
  COLLECT table2.
ENDLOOP.

More Articles:

BAPI_PO_CREATE?
function download?
Changing field description or name?
Appending Long Text?
Search Help (Table Entry)?
SPRO question for CRM know hows.?