In my project, I want to count the elements of an array as follows:

    struct Server{
       uint sernum;
       uint num;
       uint data;
    Server [] public datta;

    function count() public constant returns(uint sercount) {
        return datta.length;
    function set_data (uint Sernum ,uint Num, uint Data  ) public  {
        Server memory xserver = Server(Sernum,Num,Data);
        uint x= count();
        bool y;
            for (uint i = 0; i<datta.length;i++){
                if(datta[i].sernum == Sernum && datta[i].num==Num && datta[i].data<Data){
            if (y==true){

When I call a count function to see how many elements in the array it always gives me 1 no matter how many of the elements of the array

   print( format(contract.functions.count().call()))

How do I count the elements of an array ??

  • The good way is datta.length. You may have an issue in your algorithm. – Emmanuel Collin Aug 31 '20 at 7:12

check your test script, there is some problem with parentheses. By the way, I tried to imagine the sense of the script. I guess you call the function set_data(...) as many times as the elements you want to add, but for example if the input is always the same, in particular the Data parameter, then the element will be added only the first time because the Y boolean only in that case is TRUE, because of the line "datta[i].data<Data".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.