Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Formatting Hierarchical Internal table Dynamically (Node/ Subnodes)

former_member298409
Participant
0 Likes
2,057

Hello ABAP guru's ,

I have a requirement to show Hierarchical output like below.

LEVEL-1LEVEL-2LEVEL-3LEVEL-4LEVEL-5LEVEL-nDOCUMENT
Asserts
A-Current
A-Current1
A-current 1.1 1234
A-current 1.2 5678
A-Current2 8910
A-Current3 99999
A-Noncurrent
A-Noncurrent1
A-Noncurrent2
A-Noncurrent3

Requirement Details:

  1. 1. I am having internal table with two fields NODE,SUBNODE values like below.

Note:There is some business logic behind this Internal table.

itab_input

NODESBUNODE
AssertsA-Current
A-CurrentA-Current1
A-CurrentA-Current2
A-CurrentA-Current3
AssertsA-Noncurrent
A-NoncurrentA-Noncurrent1
A-NoncurrentA-Noncurrent2
A-NoncurrentA-Noncurrent3
A-Current1A-Current1.1
A-Current1A-Current1.2



2.Explanation about Internal Table :

  Example : NODE i have value  Asserts and it's SUBNODE is A-current

  

Now we have to take the SUBNODE of   NODE – Asserts (in our case it is A-current ) and search in internal table if any NODE name existing with name A-Current  (In our case YES we have)

Again we have to take SUBNODE of that Node A-current ( in our case it is A-current1) and search in internal table if any NODE name existing with name A-Current1  (In our case NO we have) i.e we reached maximum level. if reached the maximum levels that means we have some document numbers to that node

So from the above example we have to populate like below.

LEVEL-1LEVEL-2LEVEL-3LEVEL-3DOCUMENTNUMBER
Asserts
A-Current
A-Current1
A-Current1.11234
A-Current1.25678
A-Current2 8910
A-Current3 99999



Similarly we have to do for the rest of the values also .

Please consider below points :

  1. LEVEL is dynamic (But we know how many LEVEL's come based on another logic it may varies from scenario to scenario)
  2. Document number also we have another logic based on last subnode



I have tried lot of ways to achieve the format  but I still not yet resolved .



Kindly provide your Inputs .



Thanks In advance,

Kandulas.






1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,797

See the below code. It does not give you the exact result that you are expecting but it will give you the valid result. Check the logic in the Loop, I think that is what you are looking at.

types: begin of ty_data,

         node  type char20,

         snode type char20,

       end of ty_data,

       ty_t_data type standard table of ty_data,

       begin of ty_fdata,

         node1 type char20,

         node2 type char20,

         node3 type char20,

         node4 type char20,

         node5 type char20,

         node6 type char20,

         node7 type char20,

         node8 type char20,

       end of ty_fdata,

       ty_t_fdata type standard table of ty_fdata.

data: ls_data type ty_data,

      lt_data type ty_t_data,

      ls_fdata type ty_fdata,

      lt_fdata type ty_t_fdata.

ls_data-node  = 'Asserts'.

ls_data-snode = 'A-Current'.

append ls_data to lt_data.

ls_data-node  = 'A-Current'.

ls_data-snode = 'A-Current1'.

append ls_data to lt_data.

ls_data-node  = 'A-Current'.

ls_data-snode = 'A-Current2'.

append ls_data to lt_data.

ls_data-node  = 'A-Current'.

ls_data-snode = 'A-Current3'.

append ls_data to lt_data.

ls_data-node  = 'Asserts'.

ls_data-snode = 'A-NonCurrent'.

append ls_data to lt_data.

ls_data-node  = 'A-NonCurrent'.

ls_data-snode = 'A-NonCurrent1'.

append ls_data to lt_data.

ls_data-node  = 'A-NonCurrent'.

ls_data-snode = 'A-NonCurrent2'.

append ls_data to lt_data.

ls_data-node  = 'A-NonCurrent'.

ls_data-snode = 'A-NonCurrent3'.

append ls_data to lt_data.

ls_data-node  = 'A-Current1'.

ls_data-snode = 'A-Current1.1'.

append ls_data to lt_data.

ls_data-node  = 'A-Current1'.

ls_data-snode = 'A-Current1.2'.

append ls_data to lt_data.

data: lv_nname  type char10,

      lv_tabix  type i,

      lv_levels type i value 4,

      lv_level  type i,

      lv_clevel type char2.

field-symbols: <dyn_table> type standard table,

               <dyn_wa>,

               <dyn_field>.

loop at lt_data into ls_data.

  clear: lv_level.

  do lv_levels times.

    lv_level = lv_level + 1.

    lv_clevel = lv_level.

    concatenate 'NODE' lv_clevel into lv_nname.

    condense lv_nname.

    read table lt_fdata transporting no fields

                        with key (lv_nname) = ls_data-node.

    if sy-subrc = 0.

      exit.

    endif.

  enddo.

  if sy-subrc = 0.

    " Node has been found

    clear ls_fdata.

    lv_level = lv_level + 1.

    assign component lv_level of structure ls_fdata

                              to <dyn_field>.

    <dyn_field> = ls_data-snode.

    lv_tabix = sy-tabix + 1.

    insert ls_fdata into lt_fdata index lv_tabix.

  else.

    " Node not found

    clear ls_fdata.

    ls_fdata-node1 = ls_data-node.

    append ls_fdata to lt_fdata.

    clear ls_fdata.

    ls_fdata-node2 = ls_data-snode.

    append ls_fdata to lt_fdata.

  endif.

endloop.

11 REPLIES 11
Read only

Former Member
0 Likes
1,798

See the below code. It does not give you the exact result that you are expecting but it will give you the valid result. Check the logic in the Loop, I think that is what you are looking at.

types: begin of ty_data,

         node  type char20,

         snode type char20,

       end of ty_data,

       ty_t_data type standard table of ty_data,

       begin of ty_fdata,

         node1 type char20,

         node2 type char20,

         node3 type char20,

         node4 type char20,

         node5 type char20,

         node6 type char20,

         node7 type char20,

         node8 type char20,

       end of ty_fdata,

       ty_t_fdata type standard table of ty_fdata.

data: ls_data type ty_data,

      lt_data type ty_t_data,

      ls_fdata type ty_fdata,

      lt_fdata type ty_t_fdata.

ls_data-node  = 'Asserts'.

ls_data-snode = 'A-Current'.

append ls_data to lt_data.

ls_data-node  = 'A-Current'.

ls_data-snode = 'A-Current1'.

append ls_data to lt_data.

ls_data-node  = 'A-Current'.

ls_data-snode = 'A-Current2'.

append ls_data to lt_data.

ls_data-node  = 'A-Current'.

ls_data-snode = 'A-Current3'.

append ls_data to lt_data.

ls_data-node  = 'Asserts'.

ls_data-snode = 'A-NonCurrent'.

append ls_data to lt_data.

ls_data-node  = 'A-NonCurrent'.

ls_data-snode = 'A-NonCurrent1'.

append ls_data to lt_data.

ls_data-node  = 'A-NonCurrent'.

ls_data-snode = 'A-NonCurrent2'.

append ls_data to lt_data.

ls_data-node  = 'A-NonCurrent'.

ls_data-snode = 'A-NonCurrent3'.

append ls_data to lt_data.

ls_data-node  = 'A-Current1'.

ls_data-snode = 'A-Current1.1'.

append ls_data to lt_data.

ls_data-node  = 'A-Current1'.

ls_data-snode = 'A-Current1.2'.

append ls_data to lt_data.

data: lv_nname  type char10,

      lv_tabix  type i,

      lv_levels type i value 4,

      lv_level  type i,

      lv_clevel type char2.

field-symbols: <dyn_table> type standard table,

               <dyn_wa>,

               <dyn_field>.

loop at lt_data into ls_data.

  clear: lv_level.

  do lv_levels times.

    lv_level = lv_level + 1.

    lv_clevel = lv_level.

    concatenate 'NODE' lv_clevel into lv_nname.

    condense lv_nname.

    read table lt_fdata transporting no fields

                        with key (lv_nname) = ls_data-node.

    if sy-subrc = 0.

      exit.

    endif.

  enddo.

  if sy-subrc = 0.

    " Node has been found

    clear ls_fdata.

    lv_level = lv_level + 1.

    assign component lv_level of structure ls_fdata

                              to <dyn_field>.

    <dyn_field> = ls_data-snode.

    lv_tabix = sy-tabix + 1.

    insert ls_fdata into lt_fdata index lv_tabix.

  else.

    " Node not found

    clear ls_fdata.

    ls_fdata-node1 = ls_data-node.

    append ls_fdata to lt_fdata.

    clear ls_fdata.

    ls_fdata-node2 = ls_data-snode.

    append ls_fdata to lt_fdata.

  endif.

endloop.

Read only

0 Likes
1,797

Hello Chandra Thanks a lot for giving valuable  logic.

I have tried and it is coming like below.

NODE1NODE2NODE3NODE4NODE5NODE6NODE7NODE8
Asserts
A-NonCurrent
A-NonCurrent3
A-NonCurrent2
A-NonCurrent1
A-Current
A-Current3
A-Current2
A-Current1
A-Current1.2
A-Current1.1

But I am expecting format like below because here Hierarchy very very Important

NODE1NODE2NODE3NODE4NODE5NODE6NODE7NODE8
Asserts
A-Current
A-Current1
A-Current1.1
A-Current1.2
A-Current3
A-Current2
A-NonCurrent
A-NonCurrent3
A-NonCurrent2
A-NonCurrent1

I guess we have to change the Insert in properway ... I have tried to set by indexing but not able to achieve this.

Kindly help on this.

Thanks,

Kandulas.

Read only

0 Likes
1,797

Hello Chandra Thanks a lot for giving valuable  logic.

I have tried and it is coming like below.

NODE1NODE2NODE3NODE4NODE5NODE6NODE7NODE8
Asserts
A-NonCurrent
A-NonCurrent3
A-NonCurrent2
A-NonCurrent1
A-Current
A-Current3
A-Current2
A-Current1
A-Current1.2
A-Current1.1

But I am expecting format like below because here Hierarchy very very Important

NODE1NODE2NODE3NODE4NODE5NODE6NODE7NODE8
Asserts
A-Current
A-Current1
A-Current1.1
A-Current1.2
A-Current3
A-Current2
A-NonCurrent
A-NonCurrent3
A-NonCurrent2
A-NonCurrent1

I guess we have to change the Insert in properway ... I have tried to set by indexing but not able to achieve this.

Kindly help on this.

Thanks,

Kandulas.

Read only

0 Likes
1,797

That should not be an issue. Based on the input the result will come as you expected.

If the input as follows then the result will be as you expected.

NODE                SUBNODE

Asserts               A-NonCurrent

A-NonCurrent     A-NonCurrent3

A-NonCurrent     A-NonCurrent2

A-NonCurrent     A-NonCurrent1

Asserts               A-Current

A-Current          A-Current3

A-Current          A-Current2

A-Current           A-Current1

A-Current1          A-Current1.2

A-Current1          A-Current1.1

I am sure that the above Node/Subnode sequence will change. So, based on that the result will change.

Read only

0 Likes
1,797

Thanks Chandra, your given logic perfectly working with few small modifications

I did like below for Inserting on index basis.

after RAED statement I have taken the Index and added 1 while appending.

Now My Internal Table like below as expected

Node1Node2Node3Node4
Asserts
A-Current
A-Current1
A-Current1.1
A-Current1.2
A-Current2
A-Current3
A-NonCurrent
A-NonCurrent2
A-NonCurrent3

But In output I have to show Like below.

Node1Node2Node3Node4
AssertsA-CurrentA-Current1A-Current1.1
AssertsA-CurrentA-Current1A-Current1.2
AssertsA-CurrentA-Current2
AssertsA-CurrentA-Current3
AssertsA-NoncurrentA-NonCurrent2
AssertsA-NoncurrentA-NonCurrent3

Can you kindly help me on this .to frame output like above

Please note below.

1.Node1 and Node 2 ...n is coming as Dynamic Structure  (There is no hard coding on fields).

2.We don't want to change the existing code (which is designed to separate the levels because it contains lot od sensitive logic )

Thanks In Advance,

Kandulas.

Read only

0 Likes
1,797

I did like below for Inserting on index basis.

after RAED statement I have taken the Index and added 1 while appending.

Now My Internal Table like below as expected 

Node1Node2Node3Node4
Asserts
A-Current
A-Current1
A-Current1.1
A-Current1.2
A-Current2
A-Current3
A-NonCurrent
A-NonCurrent2
A-NonCurrent3

But In output I have to show Like below.

Node1Node2Node3Node4
AssertsA-CurrentA-Current1A-Current1.1
AssertsA-CurrentA-Current1A-Current1.2
AssertsA-CurrentA-Current2
AssertsA-CurrentA-Current3
AssertsA-NoncurrentA-NonCurrent2
AssertsA-NoncurrentA-NonCurrent3

Can you kindly help me on this .to frame output like above

Please note below.

1.Node1 and Node 2 ...n is coming as Dynamic Structure  (There is no hard coding on fields).

2.We don't want to change the existing code (which is designed to separate the levels because it contains lot od sensitive logic )

Thanks In Advance,

Kandulas.

Read only

0 Likes
1,797

Hi Chandra,

Is any possibility to achieve above format ?

Thanks,

Kandulas.

Read only

0 Likes
1,797

That should not be a big issue to achieve it. Just loop through the final internal table and arrange the data according to your requirement.

Please mark the posts as helpful or answered if any of the responses helps you in solving your problem.

Read only

0 Likes
1,797

Hi Chandra I have tried a lot but still I didn't find any exact logic to frame my Internal table format like above .

it is really helpful to  if you provide the logic.

Thanks,

Kandulas.

Read only

0 Likes
1,797

Were you able to get the solution?

Read only

former_member210008
Active Participant
0 Likes
1,797

Hi, Siva!

Did you try to use any alv tree? I think it will be better and easier. Or it must be a flat table?