Iterating over a StringMap

Paul59

Active Member
Joined
Dec 13, 2018
Location
UK
I think I'm losing the plot, I've been staring at this for an hour! :confused:

How would I iterate over this StringMap in order to access both the keys and the values?
Cerberus X:
    myList = New StringMap<Float>()
    myList.Add("Fuel", 0.99)
    myList.Add("Temperature", 80)
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
Simples.....
Cerberus X:
Strict

Function Main:Int()
    Local strMap:= New StringMap<Int>
    strMap.Add("zero", 0)
    strMap.Add("one", 1)
    strMap.Add("two", 2)
    strMap.Add("three", 3)
    
    For Local i:=Eachin strMap
        Local k:= i.Key()
        Print "Key: "+k+" = "+i.Value()
    Next
    Return 0
End
 

Paul59

Active Member
Joined
Dec 13, 2018
Location
UK
Simples.....
Thank you. That's one thing I didn't try as it doesn't seem to make sense (in Strict mode at least) insofar as no type is being supplied for i.
I haven't noticed that ':=' syntax, also used in your declarations of strMap and k, in CX before.

Amongst other things I was trying to do this:

Code:
For Local i: StringMap<Float>  =  eachin myList
which would seem to be the logical way (to me at least!) but it results in: Error : Cannot convert from Node<String,Float> to StringMap<Float>
 

Paul59

Active Member
Joined
Dec 13, 2018
Location
UK
Looking at a part of the Language Reference I've not visited before I read:

"If present, Local will create a new local index variable that only exists for the duration of the loop. In addition, IndexVariable must include the variable type, or := must be used instead of = to implicitly set the variable's type"

So what type would I specify if not using the := notation?
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
For Local i: StringMap<Float> = eachin myList
StringMap is a convenience class that basically creates a Map<String, T> object and each item within a map/list is of a type declared as Node. As there is more than one declaration of type Node, you have to use the module/name resolution specifier to identify which module the node is from; for a map it's from cerberus.map, the other declaration for Node is in cerberus.list.

Cerberus X:
Function Main:Int()
    Local strMap:= New StringMap<Int>
    strMap.Add("zero", 0)
    strMap.Add("one", 1)
    strMap.Add("two", 2)
    strMap.Add("three", 3)

    Local k:String, v:Int
    For Local i:map.Node<String,Int> = Eachin strMap
        k = i.Key()
        v = i.Value()
        Print "Key: "+k+" = "+v
    Next
    Return 0
End
Note: That data stored in a map may not be stored in the order that you think. The whole point of a map is to locate data by a key value. Iterating a map container is very expensive unless you need to parse something like json data into another custom data type.
 
Last edited:

Paul59

Active Member
Joined
Dec 13, 2018
Location
UK
StringMap is a convenience class that basically creates a Map<String, T> object and each item within a map/list is of a type declared as Node. As there is more than one declaration of type Node, you have to use the module/name resolution specifier to identify which module the node is from; for a map it's from cerberus.map, the other declaration for Node is in cerberus.list.
...
Note: That data stored in a map may not be stored in the order that you think. The whole point of a map is to locate data by a key value. Iterating a map container is very expensive unless you need to parse something like json data into another custom data type.
Got it, thanks. I was looking into a map as one way to handle a bunch of stats for display rather than having to use, for example, an array with and a separate string array holding the names of the stats (with an enumeration or group of consts to use for indexing) but it seems a bit crude. It wouldn't be called often but the order would be important for display purposes. I suppose an array of stat objects with a display name and value would also do, something like:

Enumerate FUEL, TEMP, ...

stats: Stat[]
stats[FUEL] = New Stat( "Fuel", 0.99)
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
Enumerate FUEL, TEMP, ...

stats: Stat[]
stats[FUEL] = New Stat( "Fuel", 0.99)
Why use an array when an IntMap would surf ice. That is providing that you don't you the same integer value twice as a key.
Usually a map is expensive to add and remove data, but access should be just as fast as an array.
 

Paul59

Active Member
Joined
Dec 13, 2018
Location
UK
Why use an array when an IntMap would surf ice.
Good question! I suppose there might be some other slight benefits in using a regular for loop and an array in that I might want to use the loop control variable (index) for something else rather than having to track a separate variable within a for .. eachin loop. Probably not much difference either way but food for thought - cheers :D
 
Top Bottom