0

I have an array of bytes10 inside a mapping, like this:

mapping(uint => bytes10[]) public Players;

I want to check if a certain index exists in the bytes10 array.
I tried to check it with .length but it throws opcode error when the index does not exist.

14
  • "I tried" - well how exactly did you try? And what does the mapping have to do with all of this? Doesn't sound related to your problem. Jan 14, 2020 at 15:43
  • Players[_id][i].length results in same opcode error. Jan 14, 2020 at 15:45
  • And what does Players[_id].length result with? (spoiler: 0 - does that answer your question)? Jan 14, 2020 at 15:49
  • 1
    You want - that's reasonable. The way you are doing it - that's not reasonable (I mean, according to what Solidity allows you to do). If your length is 4, then you can access the elements at indexes 0, 1, 2 and 3. If you access anything higher than 3 then the transaction is reverted! That is why I asked in the comment above - "Do you know how array indexing works?" (to which you responded sarcastically BTW). Jan 14, 2020 at 16:01
  • 1
    I asked if you knew how indexing worked, because you seemed not to. In fact, your set of comments prior to that question of mine ("length is 4" followed by "0, 1, 2, 3 working fine, 4 throws error") was a very solid proof of that. No workaround is needed here. Like I said, you should access Players[_id][i].length only for i between 0 and Players[_id].length - 1 (in your example, between 0 and 3). Jan 14, 2020 at 16:37

0

Browse other questions tagged or ask your own question.