abapinternal-tables

How to shuffle / randomize internal table in SAP


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?


Solution

  • 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.