Help with dictionary

Hi,
can you help me to remove an element in my dictionary ?
Code for create dictionary :

Divisions = New Dictionary Dim ALEast() As Text = Array("Red Sox", "Yankees", "Rays", "Orioles", "Blue Jays") Dim ALCentral() As Text = Array("Tigers", "White Sox", "Twins", "Royals", "Indians") Dim ALWest() As Text = Array("Angels", "Mariners", "Rangers", "Athletics", "Astros") Dim NLEast() As Text = Array("Mets", "Braves", "Nationals", "Marlins", "Phillies") Dim NLCentral() As Text = Array("Cardinals", "Brewers", "Reds", "Pirates", "Cubs") Dim NLWest() As Text = Array("Dodgers", "Pardres", "Giants", "Diamondbacks", "Rockies") Divisions.Value("AL East") = ALEast Divisions.Value("AL Central") = ALCentral Divisions.Value("AL West") = ALWest Divisions.Value("NL East") = NLEast Divisions.Value("NL Central") = NLCentral Divisions.Value("NL West") = NLWest

I would like to remove by code the “Red Sox” team from division “ALEast”.

Thanks for your help :slight_smile:

Jerome

[quote]Value(key As Auto, Assigns newValue As Auto)
Adds a new key-value pair to the Dictionary. If there is already an entry with the specified key, its value is altered and no exception is raised.[/quote]

Dim ALEast() As Text = Array("Yankees", "Rays", "Orioles", "Blue Jays") Divisions.Value("AL East") = ALEast

http://developer.xojo.com/xojo-core-dictionary$Remove

[quote=282703:@Sascha S]Dim ALEast() As Text = Array("Yankees", "Rays", "Orioles", "Blue Jays") Divisions.Value("AL East") = ALEast

http://developer.xojo.com/xojo-core-dictionary$Remove[/quote]

Hi Sascha,

if I use :

Divisions.Remove("Red Sox")

I have error : KeyNotFoundException

If I understood correctly, Jrme asked for removing one entry of the values.

Should be something like

Dim Entries() As Text = Divisions.Value("AL East") Dim Pos As Integer = Entries.IndexOf ("Red Sox") If Pos > -1 then Entries.Remove(pos) Divisions.Value("AL East") = Entries End If

[quote=282706:@Ulrich Bogun]If I understood correctly, Jérôme asked for removing one entry of the values.

Should be something like

Dim Entries() As Text = Divisions.Value("AL East") Dim Pos As Integer = Entries.IndexOf ("Red Sox") If Pos > -1 then Entries.Remove(pos) Divisions.Value("AL East") = Entries End If[/quote]

Thanks Ulrich :slight_smile:

[quote=282705:@JrmeLeray]Hi Sascha,

if I use :

Divisions.Remove("Red Sox")

I have error : KeyNotFoundException[/quote]

Because Red Sox is no Key :wink:

Thank you @UlrichBogun , my “solution” was assuming it’s already known in which Key the Red Sox will be in and used a quick and dirty replacement for this Key. Your solution is by far better. :slight_smile:

A “like” for the team you chose to remove. :slight_smile: