[Gambas-user] Rename a Collection Key retaining the order
Bruce Steers
bsteers4 at gmail.com
Sun Jan 9 21:12:43 CET 2022
On Sun, 9 Jan 2022 at 18:04, Charlie Ogier <charlie at cogier.com> wrote:
> Hi Bruce,
>
> 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",
> "su": "Sunday"]
> 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
> cCopy.Add(sNew[0], snew[1])
> Else
> cCopy.Add(sName, cAbbr2Weekday.Key)
> Endif
> Next
>
> For Each sName In cCopy
> Print cCopy.Key & " --> " & sName
> Next
>
> End
>
> Output: -
> mo --> Monday
> tu --> Tuesday
> we --> Wednesday
> BUSYDAY --> Thursday
> fr --> Friday
> sa --> Saturday
> su --> Sunday
>
> All the best,
>
> Charlie
>
Thanks Charlie , I think you're right, just copying over and changing the
name is much neater code than the way i think i over-thought it lol
I've now snuck it down to a little function that opperates on the
collection in the arg rather than returning a copy, for less code.
*RenameKey(myCollection, "t2", "ttt2")*
*Public Sub RenameKey(cCol As Collection, sName As String, sNewName As
String) Dim cCopy As Collection = cCol.Copy() ' copy collection
cCol.Clear For Each sVar As Variant In cCopy cCol.Add(sVar, If(sName =
cCopy.Key, sNewName, cCopy.Key)) NextEnd*
Cheers for the help :)
BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20220109/883acc99/attachment.htm>
More information about the User
mailing list