4

I have two arrays:

address[ ] addresses
uint [ ] numbers

I want to create a function that creates another array that stores the hashed value of both.

bytes32[ ] addressNumbers

for instance, if I have two addresses stored and two uints:

address[address1, address2] addresses,
uint [1, 2] numbers.

I want to hash them with keccak256(address, uint) such that i get an array:

bytes 32[ address1,1 ,address1,2 , address2,1 , address2,2 ] 
addressNumbers

I assume I need to use a nested for loop, but i am having trouble figuring out how. can any of you help me put?

2
  • Needs adaptation to the way Ethereum works. What are you going to do with the hashes? Commented Apr 11, 2017 at 20:46
  • it is in relation to another question on here: ethereum.stackexchange.com/questions/13994/… i want to join the two arrays to create voterIssues for each voter on each issue.
    – Mads
    Commented Apr 11, 2017 at 21:00

1 Answer 1

3

What you can do is treat it as a many-to-many join between two tables, Voters and Issues. The relationship is usually addressed with a third table, in this case Vote (or Ballet if it reduces confusion).

In summary, push Voters and Issues into tables (arrays, or mappings with indexes). Some patterns here: Are there well-solved and simple storage patterns for Solidity?

Pick a pattern that lets you easily confirm a Voter is actually registered and an Issue actually exists. Then hash the keys and push a Vote into the Vote table.

By "table" I mean one of the constructs described in the above-linked answer. Think about the strengths a weaknesses and choose accordingly. For the votes themselves, I would consider:

struct Vote {
  bool yayNay;
  bool isVote;
}

mapping(she3HashKey => Vote) voteStructs;

The second field would be set to true for all votes cast so you can tell a real "false" from a default 0, because ...

In my approach, you need simple functions to check if things exist:

isVoter() public constant returns(bool isIndeed) {}
isIssue() public constant returns(bool isIndeed) {}
isVote()  public constant returns(bool isIndeed) {}

That's a must or you might get tangled up in awkward problems, e.g. differentiating a default (false) from an actual "Nay".

Check the first two before you allow a new Vote.

if(!isVoter(msg.sender)) throw;
if(!isIssue(issueId)) throw;
// continue ... 

Check the third one before you do anything with a stored Vote.

Keep your tallies as you go so you can avoid iteration at all costs.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.