I have a table with some secondary indices. I want to iterate through the rows whose values are act >= 1 and discard the rest.


table holders(_self, _self.value);
auto index_by_act = holders.get_index<"byact"_n>();

for (auto i = index_by_act.lower_bound(1); i != index_by_act.end(); i++)

This still iterates over the rows with 0 value. If I get the same command with cleos:

cleos get table --index 3 --key-type i64 -L 1 contract contract table

I get the right list (no rows with act > 0). So I must be doing something wrong and I don't understand what exactly.

Can someone help me?

Thank you very much!


Ok I found the problem, I'll put it here so the next guy won't do the same.

I was zeroing the act value inside the loop, so somehow the iterator kept hitting the same row with a value < 0. The solution was to change to a while loop and instead of incrementing i, get a new lower bound (it will move the iterator forward anyway).

Not sure if this is very cpu-expensive, but it definitely does the trick.

Edit Here's how I made it work:

table holders(_self, _self.value);
auto index_by_act = holders.get_index<"byact"_n>();
auto i = index_by_act.lower_bound(0);

while (i != index_by_act.end())
// some logic here
i = index_by_act.lower_bound(1);
  • 1
    Can you share a code example for future users who find this question? Feb 4 '19 at 8:23
  • Hi! Sure (I was writing on my phone and that's why I didn't add some code). Will edit the comment
    – jpedrosous
    Feb 5 '19 at 9:18

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.