Given two contracts as follows, contract TestData
calls contract Reg
to fetch data that had already been stored in its map using get_attributes
function.
pragma solidity ^0.4.21;
contract Reg {
mapping (address=>bytes32[5]) Map;
//Filling the map
function fill_map(bytes32[5] _attributes) public {
Map[msg.sender] = _attributes;
}
//Returns data in the map corresponding to an address
function get_attributes(address _id) view public returns (bytes32[5]){
return Map[_id];
}
}
contract TestData {
address Address;
function get_address (address _Address) public{
Address = _Address;
}
//Retreive data from Reg contract
bytes32[5] fetched;
function fetch(address _reg_Adress) public view returns (bytes32[5]) {
Reg r = Reg(_reg_Adress);
fetched = r.get_attributes(Address);
return (fetched);
}
//Retreive input data from user
bytes32[5] inputs;
function getInputs(bytes32[5] _inputs) public view returns (bytes32[5]){
inputs = _inputs;
return (inputs);
}
//Compare input data with fetched data
bool[5] a = [false,false,false,false,false];
function compare() public view returns(bool[5]){
for(uint i=0;i<5;i++){
for(uint j=0;j<5;j++){
if(inputs[i] == fetched[j]){
a[i] = true;
}
}
}
return (a);
}
}
This is how I test it:
Fill the map with
["0x0","0x1","0x2","0x3","0x4"]
For user input I also input
["0x0","0x1","0x2","0x3","0x4"]
The output is correct returning [true,true,true,true,true]
.
However, if I change the input say, ["0xA","0x1","0x2","0x3","0x4"]
, the output remains [true,true,true,true,true]
although it should be [false,true,true,true,true]
.
Does something happen to the data as it is being fetched from the Reg contract? When I try testing the same code but with two input arrays rather than fetching the data, it works fine.
Also, I have been looking for other ways to do the comparisons. Is there any other efficient way other than looping?
i<5
andj<5
? You're stopping after comparing 3 elements.compare
as a transaction? If so, it will permanently modifya
, and subsequent calls will inherit the values in there. (Once an element is set totrue
, it will never go back.) You should probably just move the declaration ofa
insidecompare()
anyway.input
matches any element offetched
. If you only want to match corresponding elements, you need to drop the inner loop as suggested in that answer.