I am looking for a construct that's something like "return an list of key value pairs," which I think would be implemented as an array of mappings. Is that right?
That is, I'd like a data structure that contains a list of (key,value) that I will use as a global state linking some structs to association with another.
What I mean is something like:
// so I have place structs
struct Place {
uint8 placeid;
string nameOfPlace;
...etc
}
// and a place can contain no more than 1 banner.
// banners have all been to a place, but they aren't necessarily
// in a place right now. They may have been replaced with another
// banner.
struct Banner {
uint8 bannerid;
uint8 placeAt; // this must match a placeid
string thingTheBannerSays;
uint8 timestamp;
}
Place[] places; // list all places
mapping(uint8 => Place) placesById // given an id, return the place
Banner[] banners;
mapping(uint8 => Banner) bannersById // given an id, return the place
// what's missing for me is a data structure
// with global state of [(place0,bannerN),(place1,bannerM)]
// for all places. This is what I'm looking for.
function listBannersAtPlacesRightNow() returns (???? arrayOfMappings) {
??? mappings[] = arrayOfMappings
for(uint8 i =0; i < places.length; i++) {
maxTime[i] = 0; // declare this correctly
idWhereMaxTime[i] = 0; // gross, would rather be null
for(uint8 j=0; j < banners.length; j++) {
if(banners[j].timestamp > maxTime[i]) {
maxTime[i] = banners[j].timestamp // high water ;
idWhereMaxTime[i] = banners[j];
}
}
arrayOfMappings[i] = (places[i], idWheremaxTime[i]);
}
return arrayOfMappings;
}
Sorry, I'm brand new to this, but if this unclear, in words, I'd like some variable to maintain the global state that says "I am a place, I can have 0 or 1 banners. Here is a list of all the places and their current banners. Banners can be tied to exactly one place and never re-used."
Ugly quasi-python that might clarify ugly quasi solidity this would be something like:
places = [{"placeid": 0, "nameofplace": "home"}, {"placeid":1, "nameofplace": "center"}]
banners = [
{"bannerid": 0, "placeAt": 0, "thingthebannersays": "hello!"},
{"bannerid": 1, "placeAt": 0, "thingthebannersays": "hello again from home!"},
{"bannerid": 2, "placeAt": 0, "thingthebannersays": "this replaces 1!"},
{"bannerid": 3, "placeAt": 1, "thingthebannersays": "this is at center!"}]
bannerAtPlaces = [(0,2), #place 0 has current banner 2
(1,3) #place 1 has banner 3
]
def setBannerAtPlaces(placeid, bannerid):
# this updates bannerAtPlaces according to some rules
# such as the most-recent timestamp given in the solidity
# section
# BUT If I wanted to see which places contained which banners, I'd just
# print(bannerAtPlaces). This is the call I'm looking to repeat
# in solidity.
This is all coming up because I'd rather have the struct for Place contain a field for bannerid, but unfortunately, I want Place to be able to have 0 banners, and it looks like I cannot initialize a null for that---I must default to bannerid = 0
. But bannerid==0
is an actual bannerid. It cannot be in two places at once, it's already somewhere else, and cannot be the default for the banner at all new places.
The reason for a list specifying global states would be for rendering this in the browser easily, as a gallery showing all (Place, Banner) pairs.
I am now seeing a similar question here.