1

I recently approached the Smart contract two days ago. I'm still learning about it, and I have a lot of difficulties. Can you help me to create a variable like this photo?

It has a arrays of numbers from 1 to infinity. Each natural number has a array of characters. Each character has array of string, like ["abc","qwe"]

For example I have 2 functions:

  • One for add data, like addString(uint number, uint character, string info)
  • One for get data, like getString(uint number, uint character)

When I call addString(1,"a","abc"). It will create "a" if it does not already exist, after that it will put "abc" to array.

When I call addString(1,"a","qwe"). it will put "qwe" to array. Now the array of "a" like ["abc","qwe"]

When I call addString(1,"b","abc"). It will create "b" if it does not already exist, after that it will put "abc" to array. Now the array of "b" like ["abc"]

When I call getString(1, "a"), It will return ["abc","qwe"]

When I call getString(1, "b"), It will return ["abc"]

When I call getString(1, "c"), It will return []

This guy has problems almost like me. You can view this

Thanks for helping me

enter image description here

  • I can see how to do this but I'm puzzled by the purpose of the natural number that is 1 in every example. Is this a hidden assumption about the implementation details or an important requirement? What if it was 0 or 2, or 99999? – Rob Hitchens Aug 25 '18 at 14:15
  • @RobHitchensB9lab "It has a arrays of numbers from 1 to infinity. Each natural number has a array of characters. Each character has array of string, like ["abc","qwe"]" It's just an index into an array. (Think mapping(uint256 => string[])[]. The number is an index into the array.) – user19510 Aug 25 '18 at 15:26
  • @Truong You'll run into trouble with getString(). Solidity currently doesn't support returning arrays of arrays. (A string is an array, so returning string[] won't work.) If you can limit all your strings to a maximum of 32 characters, a common trick is to use bytes32 instead of string. – user19510 Aug 25 '18 at 15:27
  • @RobHitchensB9lab It's just an index into an array. It start with index 1 not 0. Smarx, Can you tell me more clearly, I do not know what you mean. I have research about it, but do not know how to use it to solve this problem. – Truong Nguyen Aug 25 '18 at 15:31
  • @Truong Have you written any code yet? Please share what you've written so far and where specifically you got stuck. It's hard to know how to help without just writing the code for you. – user19510 Aug 25 '18 at 15:33
  • @smarx you can view my smart contract https://rinkeby.etherscan.io/address/0x4d2a424b6b0eff42b8be9ad052193620ff8c5aa4#code – Truong Nguyen Aug 25 '18 at 15:35
  • @Truong That code seems to do part of what you want. It seems like it supports just a single string per stateID. Start by changing from string to string[]. – user19510 Aug 25 '18 at 15:39
  • Or you may want struct Shipment { mapping(uint256 => string[]) hashes; /*maps stateIDs to hashes*/ } – user19510 Aug 25 '18 at 15:41
  • Like this? https://remix.ethereum.org/#optimize=false&version=soljson-v0.4.24+commit.e67f0147.js – Truong Nguyen Aug 25 '18 at 15:43
  • I update it, you can check https://remix.ethereum.org/#optimize=true&version=soljson-v0.4.24+commit.e67f0147.js – Truong Nguyen Aug 25 '18 at 15:57

0 Answers0