Sum query on a Multi D array

In reply to sharatk:

You want to sum only some columns of your 2d array? Something like below? It was straight forward using a helper array, but I’d like to know if there is a single array solution.

module tb;
  
class twod;
  rand bit [7:0] cells[5][5];
  rand bit [7:0] trunc[5][5]; // helper array

  
  constraint c {
    // constrain the first two columns of cells to add up to 30
    foreach ( cells[r,c] ) {
      cells[r][c] inside { 5,10,15,20 };
      if ( c < 2 )
        trunc[r][c] == cells[r][c];
      else
        trunc[r][c] == 0;
    }
    
    foreach ( trunc[r,c] ) {
      if ( c == 0 ) {
        trunc[r].sum() == 30;
      }
    }
  }
  
  function void show();
      $write("trunc\n");
      foreach ( trunc[r,c] ) begin
        $write( "%3d ", trunc[r][c] );
        if ( c == 4 )
          $write("\n");
      end
      $write("cells\n");
      foreach ( cells[r,c] ) begin
        $write( "%3d ", cells[r][c] );
        if ( c == 4 )
          $write("\n");
      end
  endfunction
      
      
endclass

initial begin
  automatic twod td = new;
  $display( "randomize == %0b\n", td.randomize() );
  td.show();
end
      
endmodule
      
trunc
 20  10   0   0   0 
 10  20   0   0   0 
 10  20   0   0   0 
 15  15   0   0   0 
 20  10   0   0   0 
cells
 20  10  10  20  20 
 10  20  15  10  20 
 10  20  15  20  10 
 15  15   5  15  15 
 20  10   5   5  20