ulf.schroeter Posted October 28, 2010 Share Posted October 28, 2010 Hi UNIGINE, we have a string map for mapping between different filenames ( key = filename A, value = filename B ) which we would like to dump to a data file. Maybe a dump question, but how can we get the key for a specific map index position (haven't found anything in the documentation, just value lookup by explicitly specifying key) ? Something like this ... string filemap[] = ( "filename_A_1": filename_B_1", "filename_A_2": filename_B_2" ); for( int i=0; i<filemap.size(); i++ ) { string key = filemap.key ( i ); string value = filemap.value( i ); string mapping = format("%s|%s", key, value); // dump to file ... } Link to comment
Guest extaliones Posted October 28, 2010 Share Posted October 28, 2010 With map you have to use 'foreachkey' instead of 'for', please, see the code sample: foreachkey(string key; settings) { string value = settings[key]; } Link to comment
michael.zhang Posted October 28, 2010 Share Posted October 28, 2010 how can we get the key for a specific map index position Hey there, I struggled with this for some time before realizing how to use the foreach and foreachkey control structures. Basically you want to iterate through the map to do something with all KV pairs, not necessarily find the key by index.. as it's a map and has no indexing. You can use either foreach or foreachkey. Using foreachkey might look like this: foreachkey(string key ; filemap){ string value = filemap[key]; // do something } This kinda stuff really should be mentioned in the docs :) There was a time when I was maintaining a vector of keys that corresponded to a map's keys so I could iterate over it with a for loop :P FUN! Link to comment
ulf.schroeter Posted October 28, 2010 Author Share Posted October 28, 2010 There was a time when I was maintaining a vector of keys that corresponded to a map's keys so I could iterate over it with a for loop :) FUN! That's exactly what we did... This kinda stuff really should be mentioned in the docs Well, there is documentation for "foreachkey", but I missed that because I just used "UnigineScript\DataTypes" section. A comment/link to "foreachkey" would be quite helpful. Link to comment
michael.zhang Posted October 28, 2010 Share Posted October 28, 2010 Well, there is documentation for "foreachkey", but I missed that because I just used "UnigineScript\DataTypes" section. A comment/link to "foreachkey" would be quite helpful. Yes, that is what I meant. Document the intended usage and purpose of features, not merely their existence and behavior. :) Link to comment
Recommended Posts