2

Essentially I have several mappings that I would love to be able to access with a string, or bytes32 value on occasion, but mostly access directly

mapping(int256 => mapping(int256 => address)) public mapOfA;
mapping(int256 => mapping(int256 => address)) public mapOfB;
mapping(int256 => mapping(int256 => address)) public mapOfC;

I want to be able to access one of these mappings depending on a variable (either string or bytes32)

I have tried:

mapping(string => mapping(int256 => mapping(int256 => address)) public mapIndex;

When I try to set:

mapIndex["A"] = mapOfA;

I get the following error:

Error: Mappings cannot be assigned to.
mapIndex["A"]

Am I missing something or is in not possible to assign a map as a mappings value?

2

You can do it, but ...

First, you need another ) for this to compile. Then you get:

mapping(string => mapping(int256 => mapping(int256 => address))) public mapIndex;

... and you need to drop public.

This is non-obvious, but you'll get "unimplemented feature" from the compiler because it's unable to contruct the "free" getter requested by the public modifier.

This compiles:

mapping(string => mapping(int256 => mapping(int256 => address))) mapIndex;

The task the compiler doesn't figure out for you is how to make a getter with three arguments to find its way to a specific element. You can

pragma solidity ^0.4.19;

contract Nested {

  mapping(string => mapping(int256 => mapping(int256 => address))) mapIndex;

  function getMapIndex(string a, int256 b, int256 c) public view returns(address d) {
      return mapIndex[a][b][c];
  }    

  function setMapIndex(string a, int256 b, int256 c, address d) public returns(bool success) {
      mapIndex[a][b][c] = d;
      return true;
  }
}

Hope it helps.

  • Thanks for taking a look. Unfortunately I am more wondering if I can assign a map to a map. Rather than mapIndex[a][b][c]=[d] I want to do mapIndex[a]=otherMap (where other map already has [b][c][d] values set). Though it seems like I'll end up just reworking my ealier maps to be a part of this super map from the getgo – ExperimentsWithCode Mar 10 '18 at 18:54
  • 1
    The state variables describe how storage will be organized. You don't actually want large structures to move around (i.e. populate map[a] with othermap on the fly. That sounds expensive. You want the desired effect laid out in advance so nothing needs to move. I'm not sure it's clear that every key in the first map refers to a unique map of its own. Possibly you're looking for a different effect. What are you trying to solve for? – Rob Hitchens - B9lab Mar 10 '18 at 22:49
  • really just trying to more slowly roll out making all these maps one map since their used all over the code base. Seems like I gotta just do it all at once though. – ExperimentsWithCode Mar 12 '18 at 14:53

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.