IonicWind Software

IWBasic => General Questions => Topic started by: Bruce Peaslee on August 09, 2013, 09:16:52 AM

Title: Looping through dictionary
Post by: Bruce Peaslee on August 09, 2013, 09:16:52 AM
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.
Title: Re: Looping through dictionary
Post by: billhsln on August 09, 2013, 09:23:50 AM
Is the combo box set to sort the data automatically?  Assuming that it can be sorted (I know ListBoxes can).

Bill
Title: Re: Looping through dictionary
Post by: Bruce Peaslee on August 09, 2013, 09:29:01 AM
I tried that, but sorting the months of the year alphabetically doesn't really help  :D
Title: Re: Looping through dictionary
Post by: Brian on August 09, 2013, 09:41:49 AM
Does Bill mean to set the combo box to "no sorting" ?

Brian
Title: Re: Looping through dictionary
Post by: Bruce Peaslee on August 09, 2013, 10:01:53 AM
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."
Title: Re: Looping through dictionary
Post by: LarryMc on August 09, 2013, 02:27:49 PM
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
Title: Re: Looping through dictionary
Post by: Bruce Peaslee on August 09, 2013, 03:30:23 PM
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.
Title: Re: Looping through dictionary
Post by: LarryMc on August 09, 2013, 05:54:12 PM
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
Title: Re: Looping through dictionary
Post by: Bruce Peaslee on August 09, 2013, 08:20:17 PM
Sort of a sort.   ;)

Thanks.