Your code doesn't do anything like what you say you want to do; it simply 
removes the "cat" item and adds a "dog" item, which of course goes at the end.

The OrderedTable API does not offer mutable access to the keys, so there's 
really no way to do what you want. You could do something like this, but since 
the hash value isn't updated it will most likely break when more items are 
added to the table, so _don 't do this_:
    
    
    import hashes, tables
    
    type StringBox = ref object
        s: string
    
    proc box(s: string): StringBox = StringBox(s: s)
    
    proc `$`(sb: StringBox): string = sb.s
    #proc hash(sb: StringBox): Hash = hash(sb.s)
    
    let catbox = "cat".box
    
    var animals = {catbox: "woof", "cow".box: "moo", "opossum".box: 
"hisssss"}.toOrderedTable
    
    catbox.s = "dog"
    echo $animals
    
    
    Run

> what i thought i could do is split at that key, add the new key name and then 
> append the other half of the split... i don't know how i'd go about doing 
> this though

Seems simple enough:
    
    
    import tables
    
    var animals = {"cat": "woof", "cow": "moo", "opossum": 
"hisssss"}.toOrderedTable
    
    proc replaceKey[K, V](tab: OrderedTable[K, V], old, new: K): 
OrderedTable[K, V] =
      for (k, v) in tab.pairs:
        result.add(if k == old: new else: k, v)
    
    echo $replaceKey(animals, "cat", "dog")
    
    
    Run

Note that, if the same key appears more than once, this will replace all 
occurrences.

Reply via email to