2

I'm just trying to return the multiple value in the loop. How this can be done, if I wish to return the last 20 value of x and y. here is the code:

contract Bar{

    struct Foo{
        uint x;
        uint y;
    }
    mapping(uint => Foo[]) foo;

    function add(uint id, uint x , uint y)  {
        foo[id].push(Foo(x, y));
    }

    function get(uint id, uint index) public returns(uint, uint){
      //  return foo[id][index].x;
      {
      var a = foo[id][index].x;
      var b = foo[id][index].y;
      }
      return (a,b);
    }

    function get_last_ten(uint id) constant returns(uint[20]){
    uint[20] memory lastItems;
    for(uint i=0;i<20;i++){
        if(foo[id].length>i){
            lastItems[i] = foo[id][foo[id].length-i-1].x;
        }
    }
    return lastItems;
}
}
4
  • 3
    It's a fixed size array. @Shrey, what exactly is the problem? Sep 5 '18 at 15:42
  • 1
    Are you trying to return an array of Foo?
    – Ismael
    Sep 5 '18 at 15:58
  • yes, i want to return array with x and y. something like this. Sep 5 '18 at 18:31
  • @Rob Hitchens B9lab Hi sir I'm a great fan of your work Thank you for reponsing to my query. Actually I wish to give unique identity to a every transaction. So I gave a index value to each and every transaction. But now I wish to return all the index value but in such way that it will return all data of every individual transaction. And SIr it would be great if you help me with mapping this data on time. like mapping(uint => Time) time;. But when I return the time on transaction { foo[id].push(Foo(x, y)); time = now; } return (time); } I don't get time stamp Sep 7 '18 at 11:45
1

You can either return 2 fixed-size arrays, like this:

function getLastTen(uint id) constant returns(uint[10], uint[10]){
    uint[10] memory lastX;
    uint[10] memory lastY;
    Foo[] storage arr = foo[id];
    uint len = arr.length;
    for(uint i=0;i<20;i++){
        if(len>i){            
            lastX[20 - 1 - i] = arr[len - 1 - i].x;
            lastY[20 - 1 - i] = arr[len - 1 - i].y;
        }
    }
    return (lastX, lastY);

or interleave them in one uint[20] with something like

            lastXY[20 - 1 - i * 2] = arr[len - 1 - i].x;
            lastXY[20 - 1 - i * 2 + 1] = arr[len - 1 - i].y;

20 - 1 instead of 19 is intentional.

6
  • This giving me an error. Sep 7 '18 at 10:39
  • @shreybhardwaj would you care to post some context? What's your error message, what did you try to compile and run?
    – Utgarda
    Sep 7 '18 at 11:35
  • yeah sure. Let me explain you. Actually I wish to save the data on unique value. So I have tested with the index value. All new data / transaction on the public key. Is getting saved on new index value(1,2,3). Now I want to return all data of those index value. And when I try to compile this (uint len = arr.length) giving me an error. Sep 7 '18 at 11:38
  • Also is it possible to map the transaction on time. Something like this mapping(uint => Time) time; Sep 7 '18 at 11:40
  • Actually while compiling I'm getting an error from uint len = arr.length Sep 7 '18 at 11:47

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.