[Solved] List all keys in a Collection

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

[Solved] List all keys in a Collection

Post 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?

:(
Last edited by Mr.Dandy on Wed Jan 31, 2024 9:28 am, edited 1 time in total.
OpenOffice 4.1.12 - Windows 10
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: List all keys in a Collection

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: List all keys in a Collection

Post 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'}

AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: List all keys in a Collection

Post by JeJe »

EnumerableMap has key enumeration

viewtopic.php?p=385856#p385856
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
MrProgrammer
Moderator
Posts: 4909
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: List all keys in a Collection

Post 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 1630 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.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
Jurassic Pork
Posts: 24
Joined: Wed Oct 25, 2017 7:55 am
Location: France

Re: List all keys in a Collection

Post 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
OpenOffice 4.1.14 , LibreOffice 7.6.2.1 on Windows 11/ LibreOffice 7.3.7 on Lubuntu 22.04
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: List all keys in a Collection

Post 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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Mr.Dandy
Posts: 427
Joined: Tue Dec 11, 2012 4:22 pm

Re: List all keys in a Collection

Post by Mr.Dandy »

Thanks for your returns
I retain the code of Mr.Programmer
OpenOffice 4.1.12 - Windows 10
Post Reply