0

I have an internal table in my ABAP report, which consists of strings. Now I would like to shuffle the items in that table, i.e. randomize their order within the table.

Is there any ABAP built-in or function module available to achieve this, or do I have to manually randomize the table?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Matthias
  • 9,817
  • 14
  • 66
  • 125
  • How did you populate your table? If you did through SQL, just keep the ORDER BY out of the equation, if you used something like APPEND, just change it by "INSERT INDEX rnd" (where rnd is a random number < lines(tab)) – VXLozano Apr 20 '18 at 09:41
  • Have a look at function group F052, there are some function modules in it, to create random numbers of different types (int, p). (I believe you have to call FM RANDOM_INITIALIZE first (same function group).) Simpliest would be to have an integer type field in the internal table, assign a random number to it (something like between 1 and 10000) and SORT the internal table by this field. This won't be perfect (from the point of view of statistical randomness, but will do it. (I wonder what is the business requirement behind this...) – József Szikszai Apr 20 '18 at 09:53
  • @VXLozano Data is indeed coming from a database table. But just omitting `ORDER BY` is not sufficient, since in most cases the records still come in the same order. – Matthias Apr 20 '18 at 11:27
  • @JozsefSzikszai For the records: The business requirements are that I have a list of users that have to perform some tasks, but the order in which the users should be assigned to the tasks should be different every day. – Matthias Apr 20 '18 at 11:28

1 Answers1

1

So, from the comments, I would approach with something like (warning: this is pseudo-code, not the solution... if someone wants to expand it to "real code" feel free to do it, and I'll gladly will vote your answer as the right one, I'm just trying to help)

data: init_table, final_table, line, newindex.

SELECT INTO TABLE init_table.

LOOP AT init_table INTO line.
  newindex = random_function( lines( final_table ) + 1 ).
  INSERT line INTO final_table INDEX newindex.
ENDLOOP.
VXLozano
  • 317
  • 1
  • 7