Page 1 of 1

[Solved] List all keys in a Collection

Posted: Fri Jan 26, 2024 12:43 pm
by Mr.Dandy

Code: Select all

Sub Main

Dim dico As New Collection
dico.Add("49000", "Angers") ' key = "Angers", Item = "49000"
dico.Add("33000", "Bordeaux")
dico.Add("09000", "Foix")
dico.Add("11350", "Cucugnan")

for i = 1 to dico.Count
        sRet = sRet & dico.Item(i) & chr(10)   
next
msgbox sRet

End Sub
I know listed all items but how can I retreive all keys?

:(

Re: List all keys in a Collection

Posted: Fri Jan 26, 2024 3:56 pm
by JeJe
Include the key with the item. eg as a user type with key and data members

Edit:
or you could join key and item together as the item if item is a string and then use split to retieve them.

Re: List all keys in a Collection

Posted: Fri Jan 26, 2024 4:26 pm
by karolus
Hallo

or use a real programming language!

Code: Select all

dico = {"Angers": "49000",
        "Bordeaux":"33000",
        "Foix":"09000",
        "Cucugnan":"11350"}

print("\n".join(dico))
print(dico)
print("\n".join(dico.values()))
rev_dico = { value: key for key, value in dico.items() }
print(rev_dico)
prints:

Code: Select all

Angers
Bordeaux
Foix
Cucugnan

{'Angers': '49000', 'Bordeaux': '33000', 'Foix': '09000', 'Cucugnan': '11350'}

49000
33000
09000
11350

{'49000': 'Angers', '33000': 'Bordeaux', '09000': 'Foix', '11350': 'Cucugnan'}


Re: List all keys in a Collection

Posted: Fri Jan 26, 2024 5:38 pm
by JeJe
EnumerableMap has key enumeration

viewtopic.php?p=385856#p385856

Re: List all keys in a Collection

Posted: Sat Jan 27, 2024 7:07 pm
by MrProgrammer
Mr.Dandy wrote: Fri Jan 26, 2024 12:43 pm I know listed all items but how can I retreive all keys?
For OpenOffice or LibreOffice. For Linux, MacOS, or Windows.

Sub HashTest
Dim Hash As Object : Dim Enum As Object
Dim Key  As String : Dim Msg  As String : Dim Sep  As String
Hash = com.sun.star.container.EnumerableMap.create("string","string")
With Hash
   .put("Angers","49000") : .put("Bordeaux","33000")
   .put("Foix","09000")   : .put("Cucugnan","11350")
   Enum = .createKeyEnumeration(False)
   While Enum.hasMoreElements
      Key = Enum.nextElement
      Msg = Msg & Sep & "Key=" & Key & ", Item=" & .get(Key)
      Sep = Chr(10)
   Wend
   MsgBox Msg
End With
End Sub

202401262251.jpg
202401262251.jpg (20.52 KiB) Viewed 5811 times

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the Subject field. Select the green checkmark icon at the same time.

Re: List all keys in a Collection

Posted: Sun Jan 28, 2024 11:12 am
by Jurassic Pork
Hello,
in LibreOffice since the version 7.2 you have the ScriptForge Library (included in the installation of L.O). In this library you have the Dictionary service.
Exemple of use :

Code: Select all

Sub DicoTest()
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
Dim myDict As Variant, keys as Variant,k As String, sRet As String
myDict = CreateScriptService("Dictionary")
myDict.Add("49000", "Angers") ' key = "49000",  Item = "Angers"
myDict.Add("33000", "Bordeaux")
myDict.Add("09000", "Foix")
myDict.Add("11350", "Cucugnan")
keys = myDict.Keys
sSret = ""
for each k in keys
        sRet = sRet & k & chr(10)  
next
msgbox sRet
End Sub
Friendly, J.P

Re: List all keys in a Collection

Posted: Sun Jan 28, 2024 11:53 am
by JeJe
On Windows you can use their Dictionary object

https://learn.microsoft.com/en-us/offic ... ary-object

Code: Select all

Set oDict = CreateObject("Scripting.Dictionary")
okey = "Bat"
oValue = "nocturnal"
oDict.Add oKey, oValue
mri oDict.Keys
There's also a huge code base out there in Basic for coding your own collection object with access to the Keys.

Re: List all keys in a Collection

Posted: Wed Jan 31, 2024 9:27 am
by Mr.Dandy
Thanks for your returns
I retain the code of Mr.Programmer