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.

  • "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. – goodvibration Jan 14 '20 at 15:43
  • Players[_id][i].length results in same opcode error. – saman.shahmohamadi Jan 14 '20 at 15:45
  • And what does Players[_id].length result with? (spoiler: 0 - does that answer your question)? – goodvibration Jan 14 '20 at 15:49
  • That was funny, but was wrong :) It returns 4. – saman.shahmohamadi Jan 14 '20 at 15:53
  • And what value of i do you use in the expression Players[_id][i].length? – goodvibration Jan 14 '20 at 15:55
  • 0, 1, 2, 3 working fine, 4 throws error. – saman.shahmohamadi Jan 14 '20 at 15:56
  • Do you know how array indexing works? – goodvibration Jan 14 '20 at 15:56
  • @goodvibration Thank you, let's see if anyone else can help. – saman.shahmohamadi Jan 14 '20 at 15:57
  • HINT: You've got length = 4, and then you're trying to access 5 elements (at indexes 0, 1, 2, 3 and 4). What exactly do you expect to happen? – goodvibration Jan 14 '20 at 15:58
  • "anyone else" is going to tell you the exact same thing - you can access array of length x only at indexes between 0 and x - 1. – goodvibration Jan 14 '20 at 15:59
  • I want to check if an index exists or not. This is reasonable. – saman.shahmohamadi Jan 14 '20 at 16:00
  • 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). – goodvibration Jan 14 '20 at 16:01
  • @goodvibration Thank you, according to your comments I found a workaround for this. But man, you were sarcastic at first when you asked about how indexing works in solidity. I was seeking for an answer, not to be questioned. – saman.shahmohamadi Jan 14 '20 at 16:16
  • 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). – goodvibration Jan 14 '20 at 16:37

0 Answers0