Page 1 of 1

Easy way to rename a map element?

Posted: Sun Mar 01, 2026 7:30 pm
by jacdelad
Hi,

is there an easy way to rename a map element? A structured map, of course. Right now I copy all values one by one into the new element.

Re: Easy way to rename a map element?

Posted: Sun Mar 01, 2026 8:07 pm
by STARGÅTE
You mean rename the key?
Not to my knowledge.

But you can easily do
MyMap(NewKey) = MyMap(OldKey)
to copy the whole structure.

Re: Easy way to rename a map element?

Posted: Sun Mar 01, 2026 8:45 pm
by jacdelad
Yes, the key.
...does this not just copy the pointer to the map element? What about maps and lists within the map element?

Re: Easy way to rename a map element?

Posted: Sun Mar 01, 2026 9:22 pm
by STARGÅTE
jacdelad wrote: Sun Mar 01, 2026 8:45 pm Yes, the key.
...does this not just copy the pointer to the map element? What about maps and lists within the map element?
No, it is the short form for CopyStructure(@MyMap(OldKey), @MyMap(NewKey), MapStructure).
It is a full copy of all structure fields, string, lists etc.

Re: Easy way to rename a map element?

Posted: Sun Mar 01, 2026 11:06 pm
by idle
Better to use a map with integer to heap allocated structures then use
findmapelement deletemapelemnt and addmapelement

Code: Select all

Procedure RenameMapElement(Map mp(),oldkey.s,newkey.s) 
  Protected *ele.integer,value 
  *ele = FindMapElement(mp(),oldkey) 
  If *ele 
    value = *ele\i 
    DeleteMapElement(mp(),oldkey) 
    AddMapElement(mp(),newkey)
    mp() = value 
    ProcedureReturn mp()  
  EndIf  
EndProcedure  

Global NewMap foo.i() 

foo("abc") = 123  
foo("bcd") = 234 

RenameMapElement(foo(),"abc","cba") 
Debug foo() 

;hack it but only if it's static 

Structure mapelement 
  *next 
  key.s 
EndStructure   

*ele.mapelement = FindMapElement(foo(),"cba") - SizeOf(mapelement) 
PokeS(@*ele\key,"abc") 

ForEach foo() 
  Debug MapKey(foo()) + " " + Str(foo()) 
Next   

You can hack the mapkey name and get it in a foreach but it will break the functionality of the map so you can only do it safely when the map is static and only once.

Re: Easy way to rename a map element?

Posted: Mon Mar 02, 2026 6:07 am
by jacdelad
Thanks very much!