Skip to content

Instantly share code, notes, and snippets.

@willitscale
Created December 11, 2017 03:04
Show Gist options
  • Save willitscale/2a504d0bbfd35370ac2b864bf9f37a60 to your computer and use it in GitHub Desktop.
Save willitscale/2a504d0bbfd35370ac2b864bf9f37a60 to your computer and use it in GitHub Desktop.
Example implementation of solidity mapping
pragma solidity ^0.4.0;
import "github.com/willitscale/solidity-util/lib/Strings.sol";
contract MapExample {
using Strings for string;
// This would be the same as mapping(string => uint)
string[] mapKeys;
uint[] mapValues;
// This would be the same as map["myKey"];
function getValue(string key)
public
returns (uint) {
for(uint i = 0; i < mapKeys.length; i++) {
if (mapKeys[i].compareTo(key)) {
return mapValues[i];
}
}
// Nothing was found so an invalid key was specified
revert();
}
// This would be the same as map["myKey"] = 123;
function setValue(string key, uint value)
public {
mapKeys.push(key);
mapValues.push(value);
}
}
@srameshr
Copy link

srameshr commented Dec 11, 2017

function setValue(string key, uint value)
        public {
        mapKeys.push(key);
        mapValues.push(value);
    }

is this the same

function setValue(string key, uint value)
        public {
        mapKeys[key] = mapValues[value];
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment