Easy way to rename a map element?

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 2114
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Easy way to rename a map element?

Post 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.
Good morning, that's a nice tnetennba!

PureBasic 6.30/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Raspi 400/500
User avatar
STARGÅTE
Addict
Addict
Posts: 2315
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Easy way to rename a map element?

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
jacdelad
Addict
Addict
Posts: 2114
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Easy way to rename a map element?

Post 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?
Good morning, that's a nice tnetennba!

PureBasic 6.30/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Raspi 400/500
User avatar
STARGÅTE
Addict
Addict
Posts: 2315
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Easy way to rename a map element?

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
idle
Always Here
Always Here
Posts: 6241
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Easy way to rename a map element?

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 2114
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Easy way to rename a map element?

Post by jacdelad »

Thanks very much!
Good morning, that's a nice tnetennba!

PureBasic 6.30/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Raspi 400/500
Post Reply