0

I wish to map the time to the address key. But currently it is working on the index key only. I just want to map time as well. Means the current time. So that I can able to get the record with time as well.

pragma solidity ^0.4.17;

contract Experment2 {
uint public creationTime = now;
struct Record {
  // address owner;
   //uint id;
   string name;
   string senderaddress;
   string reciveraddress;
   string scheduletime;
   string scheduledate;
  // uint Index;
   uint time;
}
mapping (uint => Record[]) records;


function setRec (uint id, string name, string senderaddress, string reciveraddress, string scheduletime, string scheduledate, uint time) public {
     records[id].push(Record(name, senderaddress, reciveraddress, scheduletime, scheduledate, time =now));
}

function getrec(uint id , uint index) constant returns(string, string, string, string, string, uint){ {
    var a = records[id][index].name;
    var b = records[id][index].senderaddress;
    var c = records[id][index].reciveraddress;
    var d = records[id][index].scheduletime;
    var e = records[id][index].scheduledate;
    var f = records[id][index].time;

}
return(a,b,c,d,e,f);
}


function get_last_ten(uint id) constant returns (uint[10]){
    //uint[10] memory lastItem1;
    uint[10] memory lastItem2;
    for(uint i=0;i<10;i++){
        if(records[id].length>i){
          //  lastItem1[i] = records[id][records[id].length-i-1].index;
            lastItem2[i] = records[id][records[id].length-i-1].time;
        }
    }
    return (lastItem2);
}
}

Like in the above code the mapping is only through with index value. I can onl manage to return the current time. But I want to map the data on time as well.

So that I can run

function getrec(uint id , uint time) constant returns(string, string, string, string, string, uint){ {
    var a = records[id][time].name;
    var b = records[id][time].senderaddress;
    var c = records[id][time].reciveraddress;
    var d = records[id][time].scheduletime;
    var e = records[id][time].scheduledate;
    var f = records[id][time].time;

}
return(a,b,c,d,e,f);
}

bumped to the homepage by Community yesterday

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • 1
    Rather than copy past your entire code, can you provide a minimal, complete, verifiable example of what you are trying to achieve? Show us an example input and example output. – Shawn Tabrizi Oct 5 '18 at 18:46
  • Well I was trying to explain things. Thanks for not answering and suggestion. But I have not asked the question from particular person. It's good you have understood the question but this for those who really wish to help. And also for those who wish to learn something new. And they learn through these questions. Because they try to find as much as they can. – shrey bhardwaj Oct 6 '18 at 15:19
0

You can use an indirect index from id to time to index (to simplify I'd assume time are not repeated).

mapping (uint => mapping (uint => uint)) recordIndex;

Then you can access a record from time using the index obtained.

function getrec(uint id , uint time) view returns (string, string, string, string, string, uint) {
    uint index = recordIndex[id][time];
    // We use index = 0 to indicate an invalid time
    require(index > 0, "invalid time");
    index -= 1;
    string a = records[id][index].name;
    string b = records[id][index].senderaddress;
    string c = records[id][index].reciveraddress;
    string d = records[id][index].scheduletime;
    string e = records[id][index].scheduledate;
    uint f = records[id][index].time;

    return (a, b, c, d, e, f);
}

Now when adding a new record you also have to update the index.

function setRec (uint id, string name, string senderaddress, string reciveraddress, string scheduletime, string scheduledate, uint time) public {
     uint index = records[id].push(Record(name, senderaddress, reciveraddress, scheduletime, scheduledate, time =now));
     // push returns the length of the array
     // or equivalently position+1
     recordIndex[id][now] = index;
}

You probaly can solve the problem of records with duplicated times if you use an array to store indexes.

  • This giving me an error on uint index = recordIndex[id][time]; – shrey bhardwaj Oct 11 '18 at 8:34
  • What does the error say? – Ismael Oct 11 '18 at 14:37
  • CompilerError: Stack too deep, try removing local variables. uint index = recordIndex[id][time]; – shrey bhardwaj Oct 14 '18 at 2:58
  • This error is caused if you have too many local variables in your funciton, you can either split your function, use memory variables, storage variables or use fewer variables. – Ismael Oct 14 '18 at 20:59
  • Hi Ismael, I, sorry for the pain. but its now working. Can you please spare some time check this. It will be great help for me. – shrey bhardwaj Oct 16 '18 at 10:50

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.