October 29, 2025, 01:41:42 PM

News:

IWBasic runs in Windows 11!


Looping through dictionary

Started by Bruce Peaslee, August 09, 2013, 09:16:52 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bruce Peaslee

I have created an associative array (dictionary) that matches months of the year with a number

DictAdd(g_pMonthDictionary,"February",  "02")

I want to loop through the months (e.g., "February") and add them to a combo box. However, when I loop, the values do not come out in the same order as I entered them. (I current add them manually.)

Any ideas?

Thanks.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

billhsln

Is the combo box set to sort the data automatically?  Assuming that it can be sorted (I know ListBoxes can).

Bill
When all else fails, get a bigger hammer.

Bruce Peaslee

I tried that, but sorting the months of the year alphabetically doesn't really help  :D
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Brian

Does Bill mean to set the combo box to "no sorting" ?

Brian

Bruce Peaslee

Quote from: Brian Pugh on August 09, 2013, 09:41:49 AM
Does Bill mean to set the combo box to "no sorting" ?

Brian

If I set the box to sort I get something like " April", "August", etc. If not, I get whatever DictGetNextAssoc() provides in the loop. The keys come out in what I assume is an internal order which is not the order in which I entered them. Per the help file for DictGetNextAssoc(): "Note that the position sequence is not necessarily the same as the key value sequence."
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

LarryMc

window win
mydict = DictCreate(100)
dictAdd(mydict,"01","Jan")
DictAdd(mydict,"02","Feb")
DictAdd(mydict,"03","Mar")
DictAdd(mydict,"04","Apr")
DictAdd(mydict,"05","May")
DictAdd(mydict,"06","Jun")
DictAdd(mydict,"07","Jul")
DictAdd(mydict,"08","Aug")
DictAdd(mydict,"09","Sep")
DictAdd(mydict,"10","Oct")
DictAdd(mydict,"11","Nov")
DictAdd(mydict,"12","Dec")
openwindow win,0,0,640,400,@SIZE|@MINBOX|@MAXBOX,0,"Dictionary to Combobox",&mainwindow
control win,@combobox,"",20,20,120,320,@CTCOMBODROPDOWN,1
for x= 1 to 12
text=DictLookup(myDict,right$("0"+ltrim$(str$(x)),2))
ADDSTRING win, 1, text
next x
waituntil iswindowclosed(win)
DictFree(mydict)
end

sub mainwindow
select @MESSAGE
case @IDCREATE
centerwindow win
case @IDCLOSEWINDOW
CLOSEWINDOW win
case @IDMENUPICK
select @MENUNUM
case 3
CLOSEWINDOW win
endselect
endselect
return
endsub
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bruce Peaslee

Larry,

That provides a way to get at the months, but I am doing it backwards.

g_pMonthDictionary = DictCreate(12)
   DictAdd(g_pMonthDictionary,"January",   "01")
   DictAdd(g_pMonthDictionary,"February",  "02")
   DictAdd(g_pMonthDictionary,"March",     "03")
   DictAdd(g_pMonthDictionary,"April",     "04")
   ...


The idea is to let the user pick the month in plain English, but let me manipulate the month number. Your way gives the month if supplied the number, I want the number if supplied the month. The are many ways to do it, but your way, if I go go in the other direction, is the most direct.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

LarryMc

So, you can do this
window win
mydict = DictCreate(100)
dictAdd(mydict,"Jan","01")
DictAdd(mydict,"Feb","02")
DictAdd(mydict,"Mar","03")
DictAdd(mydict,"Apr","04")
DictAdd(mydict,"May","05")
DictAdd(mydict,"Jun","06")
DictAdd(mydict,"Jul","07")
DictAdd(mydict,"Aug","08")
DictAdd(mydict,"Sep","09")
DictAdd(mydict,"Oct","10")
DictAdd(mydict,"Nov","11")
DictAdd(mydict,"Dec","12")
openwindow win,0,0,640,400,@SIZE|@MINBOX|@MAXBOX,0,"Dictionary to Combobox",&mainwindow
control win,@combobox,"",20,20,120,320,@CTCOMBODROPDOWN,1
for x= 1 to 12
pos = DictGetStartAssoc(myDict)
WHILE pos
pAssoc = DictGetNextAssoc(myDict,pos)
if (int)val(DictGetValue(pAssoc)) = x
ADDSTRING win, 1, DictGetKey(pAssoc)
break
endif
ENDWHILE
next x
waituntil iswindowclosed(win)
DictFree(mydict)
end
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Bruce Peaslee

Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles