0

I have issue in the following smartcontract.

I have defined a struct which includes an string array. And I have defined a mapping for this struct.

struct Stonedetails {

        uint stoneweight;
        string stoneimagehash;
        string stoneidbywholesaledealer;
        string[] stoneidsbycutter;


}

mapping (uint=>Stonedetails) public stonedetailsTable;

I have stored the values in struct using the following methods inside a function by passing parameters.

 stonedetailsTable[stoneid].stoneweight =stoneweight;
 stonedetailsTable[stoneid].stoneimagehash =stoneimagehash;
 stonedetailsTable[stoneid].stoneidbywholesaledealer =stoneidbywholesaledealer;

How can I store the values in string array? Is it the way to store the values one by one?

stonedetailsTable[stoneid1].stoneidsbycutter.push(stoneidbycuttervalue);

How can I find the length of array using stoneidsbycutter.length?

  • I'm not sure what your question is. Is the code you shared not working? Is stonedetailsTable[stoneid].stoneidsbycutter.length not working? – user19510 Sep 28 '17 at 7:18
  • @smarx I tried to find length ,but it returns value as '0'...Is the way of insertion I used is correct?? – Crissi Mariam Robert Sep 28 '17 at 7:32
  • It looks okay to me. Can you share some full code that exhibits the problem? (E.g. a push followed by checking the length.) – user19510 Sep 28 '17 at 8:04
2

Your code looks correct. I tested the following and got a length of 1 as expected:

pragma solidity ^0.4.17;

contract Test {
    struct Stonedetails {
        uint stoneweight;
        string stoneimagehash;
        string stoneidbywholesaledealer;
        string[] stoneidsbycutter;
    }

    mapping (uint=>Stonedetails) public stonedetailsTable;

    function Test() public {
        stonedetailsTable[0].stoneidsbycutter.push("hello");
    }

    function getLength() public constant returns (uint256) {
        return stonedetailsTable[0].stoneidsbycutter.length; // returns 1
    }
}

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.