32-bit variable is present. it will has some random value. we need to return the index value whichever has the 1 for the first time. ex:68 number is there, if you write in binary format that is 01101000. in this it hits 1 for the first time

I am executing the below code. But getting the error

// Code your design here
class packet;
  rand bit  [31:0] addr [];
  int q[$]; 
  int i;
  constraint c1 {addr.size()==32;}
  function int prime( addr[i]);
    for (int i=0;i<32;i++)
      begin
        if(addr[i]==1)
          return 1;
      end
  endfunction

  function void post_randomize();
    if(prime(addr[i]))
      q.push_back(i);
  $display("the value of first 1 is %p", q);
    
  endfunction
endclass
                 
                 
                 module abc();
                   packet pkt;
                   initial begin
                     pkt=new();
                     pkt.randomize();
                   end
                 endmodule

Error is as below Error-[TCF-CETE] Cannot evaluate the expression
design.sv, 7
“this.i”
Cannot evaluate the expression in single dimension value.
The expression must be compile time constant.

Can anyone pls help me how to proceed

I executed the problem statement as below:

class abc;
  
  rand bit[31:0]  data;
  
  task run();
    $display("data =%b", data);
    for(int i =0; i<32; i++) begin //{
      if(data[i] == 1) begin 
        $display("index =%0d", i);
        break;
      end
    end //}
  endtask
endclass: abc
      
      
      
module tb;
  abc abc_inst;
  
  initial begin 
    abc_inst= new();
    abc_inst.randomize();
    abc_inst.run(); 
    
  end
endmodule: tb


There are few issues in above code:

  1. You are pushing the array location in the queue and not the position where first 1 occurred. For example, arr[5] = 0000_0000_0000_0000_0000_0000_0000_0100
    In this case first 1 lies at position 2, but you are pushing 5 in the queue because you are iterating for 5th array.
    I have made few changes and the result is coming as expected. Hope it helps.
class packet;
  rand bit  [31:0] addr [];
  int q[$]; 
  int y, j;
  constraint c1 {addr.size()==32;}
  
  function int prime(bit[31:0] addr);
    for (j=0;j<32;j++)begin
        if(addr[j]==1)
        break;
    end
    return j;
  endfunction

  function void post_randomize();
    foreach(addr[i])
      $display("arr[%0d]=%b", i, addr[i]);
    
    foreach(addr[i]) begin //{
     y = prime(addr[i]);
     $display("y =%0p", y);
     q.push_back(y);
    end//}
    $display("the value of first 1 is %p", q);
  endfunction
endclass
                 
                 
  module abc();
       packet pkt;
       initial begin
         pkt=new();
         pkt.randomize();
       end
  endmodule