[quote author=Presto Ragu link=topic=289.msg1692#msg1692 date=1153974888]
Well, once I got over the problem of things not working like I thought they should, I have figured out that it
should be possible to get it to work.
It is very convoluted, and frankly there are problems with it...[/quote]
Tirian is correct about your data structure not being conducive to maps. And his suggestion of having the item # being the first dimension and simply storing a dummy boolean as the ultimate value is good; it lets you immediately look up the food item and the rest of your lookup is a way to get such and such an attribute of that food item, in the absence of structures or records.
However, in the interest of education, I'll point out some problems with your initial code:
Code:
foreach key1 in FoodItem
{
foreach key2 in FoodItem
{
foreach key3 in FoodItem
{
foreach key4 in FoodItem
{
if( FoodItem[" + key1 + ", " + key2 + ", " + key3 + ", " + key4 + "] == testitem)
{
founditem = true;
itemfullness = key1;
itemstat = key2;
itemminadventures = key3;
itemmaxadventures = key4;
}
}
}
}
}
If I were to do this, here's what I would do:
Code:
foreach key1 in FoodItem
foreach key2 in FoodItem[ key1 ]
foreach key3 in FoodItem [ key1, key2]
foreach key4 in FoodItem[ key1, key2, key3 ]
i f ( FoodItem[ key1, key2, key3, key4 ] == testitem)
{
// ...do your processing here...
return;
}
I may change foreach to allow this:
Code:
foreach key1, ..., key4 in FoodItem
<statement or block that can access key1 ... key 4>
...which is just syntactic sugar for the nested set of loops. It'd be dangerous were I to do so, since the programmer might get lulled into the false sense that it's an efficient way to do things, which it isn't, particularly; if you have <m> dimensions, it's O(N^m).
I continue to be puzzled by your statement that you'd prefer "the simplicity of arrays". I see nothing in your algorithm that would be "simpler" if you had a Pascal array rather than an ASH map.