[Gambas-user] Rename a Collection Key retaining the order
charlie at cogier.com
Sun Jan 9 18:38:46 CET 2022
My thought was to rebuild the Collection. That way you could not put in
the one you want to change and add the one you do. The code below will
change 'th' to "BUSYDAY".
Public Sub Form_Open()
Dim cAbbr2Weekday As Collection = ["mo": "Monday", "tu": "Tuesday",
"we": "Wednesday", "th": "Thursday", "fr": "Friday", "sa": "Saturday",
Dim sName As String
Dim cCopy As New Collection
Dim sChange As String = "th"
Dim sNew As String = ["Thursday", "BUSYDAY"]
For Each sName In cAbbr2Weekday
If cAbbr2Weekday.Key = sChange Then
For Each sName In cCopy
Print cCopy.Key & " --> " & sName
mo --> Monday
tu --> Tuesday
we --> Wednesday
BUSYDAY --> Thursday
fr --> Friday
sa --> Saturday
su --> Sunday
All the best,
On 09/01/2022 13:54, Bruce Steers wrote:
> Is there a simple way to rename a collection key name but retain the
> I found i can rename it using Swap but it pops the item at the and of
> the collection index.
> Best i have so far is this fuction that copies and returns the
> collection with a preserved order.
> so if my second collection item had the key "t2" I can do this...
> *cCollection = RenameKey(cCollection, "t2", "ttt2")
> Public Sub RenameKey(c As Collection, sName As String, sNewName As
> String) As Collection
> Dim i As Integer = c.Keys.Find(sName) ' find key position
> Swap c[sName], c[sNewName] ' swap key names
> ' make a new collection preserving original order
> Dim nc As New Collection
> For cnt As Integer = 0 To c.Count - 1
> If cnt = i Then ' insert point so move last added key to our position
> nc.Add(c[c.Keys[c.Keys.Max]], c.Keys[c.Keys.Max])
> Else If cnt > i Then 'after insert so shunt keys back one
> nc.Add(c[c.Keys[cnt - 1]], c.Keys[cnt - 1])
> Else ' before insert so just copy
> nc.Add(c[c.Keys[cnt]], c.Keys[cnt])
> Return nc
> Am i missing a one line command or is that the only way to do it ?
> Cheers all :)
> ----[http://gambaswiki.org/wiki/doc/netiquette ]----
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the User