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

Internal table permutations: for experts

Former Member
0 Likes
948

Hi all,

i have an internal table with 10 values.

I want to have all permutations. There are 10! possibilities.

How can i calculate this?

regards

8 REPLIES 8
Read only

Former Member
0 Likes
729

hi check this ...you will see all the permutations...

http://service.sap.com/instguides.

regards,

venkat.

Read only

0 Likes
729

Hi,

i can't follow you.

Are you kidding me?

regards

Read only

Former Member
0 Likes
729

Could you explain your requirement further?

Read only

Former Member
0 Likes
729

I'm not sure about what you need...It could something like this???


TYPES: BEGIN OF ty_table,
       value TYPE string,
       END OF ty_table.

DATA: t_table TYPE STANDARD TABLE OF ty_table
      WITH HEADER LINE.

DATA: counter TYPE sy-tabix,
      count TYPE sy-tabix,
      value TYPE string,
      perm TYPE string.

FIELD-SYMBOLS: <fs_table> LIKE LINE OF t_table.

DO 10 TIMES.
  counter = counter + 1.
  t_table-value = counter.
  APPEND t_table.
ENDDO.

LOOP AT t_table ASSIGNING <fs_table>.
  CLEAR count.
  value = <fs_table>-value.
  CONCATENATE perm value
  INTO perm SEPARATED BY '-'.
  DO 10 TIMES.
    count = count + 1.
    READ TABLE t_table INDEX count.
    IF value NE t_table-value.
      CONCATENATE perm t_table-value
      INTO perm SEPARATED BY '-'.
      CONDENSE perm NO-GAPS.
    ENDIF.
  ENDDO.
  SHIFT perm LEFT DELETING LEADING '-'.
  WRITE:/ perm.
  CLEAR perm.
ENDLOOP.

Result...


1-2-3-4-5-6-7-8-9-10 
2-1-3-4-5-6-7-8-9-10 
3-1-2-4-5-6-7-8-9-10 
4-1-2-3-5-6-7-8-9-10 
5-1-2-3-4-6-7-8-9-10 
6-1-2-3-4-5-7-8-9-10 
7-1-2-3-4-5-6-8-9-10 
8-1-2-3-4-5-6-7-9-10 
9-1-2-3-4-5-6-7-8-10 
10-1-2-3-4-5-6-7-8-9 

Greetings,

Blag.

Read only

0 Likes
729

hi,

thanks.

Yes i need something like that.

I have one value e. g. 5000.

And in the internal table i have sub values.

e.g. 500, 400, 350, 70, 80 ....

I have to find the optimal combination of the sub values to fulfill the whole value.

For that issue i want to build all permutations... to get the optimal combination....

regards

Read only

Former Member
0 Likes
729

It took me some time, but I made this


types: begin of ty_tab,
text,
flag,
end of ty_tab.


data itab type table of ty_tab with header line.
data: result(200).
data: i_lines type p.
data: permutations type p.

data: curr_lenght type p value 0.
itab-text = 'a'.
append itab.
itab-text = 'b'.
append itab.
itab-text = 'c'.
append itab.
itab-text = 'd'.
append itab.


describe table itab lines i_lines.

perform loopear.
permutations = sy-linno - 2.
write: / 'number of permutations: ', permutations.

form loopear.
data: wa_itab type ty_tab.

do i_lines times.

read table itab into wa_itab index sy-index.
read table itab into wa_itab with key text = wa_itab-text flag = 'X'.
if sy-subrc ne 0.
wa_itab-flag = 'X'.
modify itab from wa_itab index sy-index.
result+curr_lenght(1) = wa_itab-text.
curr_lenght = curr_lenght + 1.
perform loopear.

if curr_lenght eq i_lines.
write / result.
endif.

wa_itab-flag = ''.
modify itab from wa_itab index sy-index.
curr_lenght = curr_lenght - 1.
endif.

enddo.


endform.

Sadly it only works with unique values, it's working up to 7!.

It was something quick, maybe you can modify it to your requirements

Read only

0 Likes
729

Nice -:D

Greetings,

Blag.

Read only

0 Likes
729

Thanks, I made a bet with some colleagues that I could make it.

It's not exactly what he asks for, but I guess he can modify it now that the permutations are working. Saddly that's the best I can do while at work.

And I guess it can work with loops also, but I became frustrated with sy-tabix and decided to put that crazy do before noticing a little mistake I made. Didn't tried it with 10! I'm afraid it may take a lot of time.