Is there an 'AskArrayList'?
Keywords: AskArrayList
Question:
It would really be nice if there were a function like AskItemList() that would allow you to pick an item, but do it from an array.
For example, if we have the following array:
1 2 3
1 Marty blue 17
2 Jeff green 9
3 Bill yellow 35
I would like to display all three lines and have the user pick one.
Currently (unless I'm missing something), I would have to create a variable, manually popualte it with the array:
a = "Marty,blue,17@TABJeff,green,9@TABBill,yellow,35"
display the AskItemList box, figure out which they chose and (difficult) which element of the array it is, then I can work on it.
Am I missing something? Is there an easier way?
Answer:
Hmmm. It should not be *too* difficult, but it's a kind of too general function to add to WinBatch. But once
you get a UDF perfected, you could use it where ever you like. ... try the following udf ...
#DefineFunction udfArrayAskRow (title, array, sortmode, selectmode, askmode)
sortmode = max(@unsorted,min(@sorted,sortmode))
selectmode = max(@single,min(@extended,selectmode))
askmode = max(0,min(1,askmode))
delimitem = @tab
delimrow = "|"
dimmin = 1
dimmax = 2
dim = ArrInfo(array,0)
If (dim>dimmax) then return ("")
If (dim<dimmin) then return ("")
For i=1 to dimmax
e%i%=max(ArrInfo(array,i)-1,0)
Next
list = ""
For d1=0 to e1
row = ""
For d2=0 to e2
index = ""
For i=1 to dim
index = ItemInsert(d%i%,-1,index,",")
Next
row = ItemInsert(array[%index%],-1,row,delimitem)
Next
row = ItemInsert(d1,-1,row,delimitem)
list = ItemInsert(row,-1,list,delimrow)
Next
resultlist = ""
rowlist = AskItemList(title,list,delimrow,sortmode,selectmode)
Select askmode
Case 0
icount = ItemCount(rowlist,delimrow)
For i=1 to icount
rowitem = ItemExtract(i,rowlist,delimrow)
rownum = ItemExtract(-1,rowitem,delimitem)
resultlist = ItemInsert(rownum,-1,resultlist,delimrow)
Next
Break
Case 1
resultlist = rowlist
Break
EndSelect
:CANCEL
Return (resultlist)
; parameters:
; title = title of the AskItemList box.
; array = 1-dim or 2-dim array variable.
; sortmode = @sorted for an alphabetic list.
; sortmode = @unsorted to display the list of items as is.
; selectmode = @single to limit selection to one item.
; selectmode = @multiple to allow selection of more than one item.
; selectmode = @extended to allow selection of multiple items by extending the selection with the mouse or shift key.
; askmode = 0 to return a list of selected array row index/es delimited by "|"
; askmode = 1 to return a list of selected array row/s delimited by "|"
; If array dimension is not in the allowed range (1..2) then this udf returns an empty string "".
; The function IntControl (63, p1, p2, p3, p4) can be used to set the display coordinates for AskItemList.
; (IntControl 63 can be useful to cut resp. hide the rightmost array column item while displaying the AskItemList box.)
;
; Detlev Dalitz.20020521
#EndFunction
; --- test ---
; create 2-dim test array with d1 rows and d2 columns
d1 = 4
d2 = 4
arr = ArrDimension(d1,d2)
arr[0,0] = "Mickey"
arr[0,1] = "Mouse"
arr[0,2] = 11
arr[0,3] = "MM"
arr[1,0] = "Goofy"
arr[1,1] = "Dog"
arr[1,2] = 22
arr[1,3] = "GD"
arr[2,0] = "Carlo"
arr[2,1] = "Cat"
arr[2,2] = 33
arr[2,3] = "CC"
arr[3,0] = "Dagobert"
arr[3,1] = "Duck"
arr[3,2] = 44
arr[3,3] = "DD"
; another testcase
; create 1-dim test array with d1 rows
;d1 = 4
;arr = ArrDimension(d1)
;
;arr[0] = "Mickey"
;arr[1] = "Goofy"
;arr[2] = "Carlo"
;arr[3] = "Dagobert"
MsgTitle = "Demo udfArrayAskRow (title, arrayvar, sortmode, selectmode, askmode)"
; test 1.0
title = "Test 1.0, select single array row (index)"
row = udfArrayAskRow (title, arr, @unsorted, @single, 0)
MsgText = row
MsgText = StrCat(title,@crlf,MsgText)
Message(MsgTitle,MsgText)
; test 1.1
title = "Test 1.1, select single array row"
rowlist = udfArrayAskRow (title, arr, @unsorted, @single, 1)
MsgText = rowlist
MsgText = StrCat(title,@crlf,MsgText)
Message(MsgTitle,MsgText)
; test 2.0
title = "Test 2.0, select multiple array row/s (index)"
rowlist = udfArrayAskRow (title, arr, @unsorted, @multiple, 0)
MsgText = StrReplace(rowlist,"|",@crlf)
MsgText = StrCat(title,@crlf,MsgText)
Message(MsgTitle,MsgText)
; test 2.1
title = "Test 2.1, select multiple array row/s"
rowlist = udfArrayAskRow (title, arr, @unsorted, @multiple, 1)
MsgText = StrReplace(rowlist,"|",@crlf)
MsgText = StrCat(title,@crlf,MsgText)
Message(MsgTitle,MsgText)
; test 3.0
title = "Test 3.0, select extended array row/s (index)"
rowlist = udfArrayAskRow (title, arr, @unsorted, @extended, 0)
MsgText = StrReplace(rowlist,"|",@crlf)
MsgText = StrCat(title,@crlf,MsgText)
Message(MsgTitle,MsgText)
; test 3.1
title = "Test 3.1, select extended array row/s"
rowlist = udfArrayAskRow (title, arr, @unsorted, @extended, 1)
MsgText = StrReplace(rowlist,"|",@crlf)
MsgText = StrCat(title,@crlf,MsgText)
Message(MsgTitle,MsgText)
; you can do the tests with "sortmode = @sorted" too.
Exit
Article ID: W15106