• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Iterating over a StringMap

Paul59

Active member
CX Code Contributor
Joined
Dec 13, 2018
Messages
384
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:
    myList = New StringMap<Float>()
    myList.Add("Fuel", 0.99)
    myList.Add("Temperature", 80)
 
Simples.....
Cerberus:
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
 
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>
 
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?
 
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:
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:
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)
 
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.
 
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
 
Back
Top Bottom