Page 1 of 1

How to put the contents of an Array into a ListBox

Posted: Wed Jan 23, 2013 7:52 pm
by Traxgeek
Hi,

Struggling again...

What is the simplest way to take the lines of a pre-populated 1 dimensional array and put all of them into a List/Combo Box please ?

No problems populating the List/Combo Box in the IDE but I need to do it at run time.

So, I have an array populated with items depending upon the language used and I wish to populate a List/Combo Box with these items so the user may select the option they need...

I have tried various things but keep coming up with either nothing or just the first item of my array in my List/Combo Box.

This works BUt it reallyi is UGLY and awfully LONG...

Code: Select all

   put 0 into N
   put empty into button "lstPeople" of group "theGroup"
   repeat until N= 32
      add 1 to N
      if gsPeople[N] is not empty then
          if n > 1 then put cr after button "lstPeople" of group "theGroup"
         put gsPeople[N] after button "lstPeople" of group "theGroup"
      end if
   end repeat
TIA

Regards.

Re: How to put the contents of an Array into a ListBox

Posted: Wed Jan 23, 2013 10:25 pm
by dunbarx
Hi.

You are certainly on the right track, in that the contents of a combo box button will become the menuItems of that button.

Your script could be tightened, but looks sound. Ugly? Verbose, but not ugly.

Try this:

Code: Select all

put empty into button "lstPeople" of group "theGroup"
      repeat with N = 1 to 32
            if gsPeople[N] is not empty and N > 1 then put gsPeople[N] & cr after button "lstPeople" of group "theGroup"
      end repeat
delete last char of button "lstPeople" of group "theGroup" --lose last return
No better than yours. Just shorter. There might be a lesson or two if you compare your script with mine. Is it prettier?

Craig Newman

Re: How to put the contents of an Array into a ListBox

Posted: Wed Jan 23, 2013 10:37 pm
by bn
Hi Traxgeek,

if speed is your concern than this might be a bit faster

Code: Select all

   put the keys of gsPeople into tKeys
   sort tKeys numeric ascending -- get the entries sorted
   repeat for each line aLine in tKeys
      put gsPeople [aLine] & cr after tCollect
   end repeat
   delete last char of tCollect -- a return
   filter tCollect without empty -- get rid of empty lines
   put tCollect into button "lstPeople" of group "theGroup"
Kind regards

Bernd

edit in a prior versions were some mistakes in the code, forgot to query the array :) and misspelled tCollect at one place

Re: How to put the contents of an Array into a ListBox

Posted: Wed Jan 23, 2013 10:51 pm
by mwieder
Craig-

Code: Select all

          repeat with N = 1 to 32
                if gsPeople[N] is not empty and N > 1 then put gsPeople[N] & cr after button "lstPeople" of group "theGroup"
          end repeat
Er... no... you'll miss the first key that way.

Re: How to put the contents of an Array into a ListBox

Posted: Wed Jan 23, 2013 11:41 pm
by dunbarx
Mark.

His script also ignores the first key.

TraxGeek, is this what you wanted?

Anyway, I was after beauty, not truth.

Craig

Re: How to put the contents of an Array into a ListBox

Posted: Thu Jan 24, 2013 10:00 am
by Traxgeek
Superb.

Thanks a million - both of you.

Perfect demo of the 'Repeat WITH'

For some reason, I hadn't appreciated this 'with' part... excellent.

And 'delete last char of [some variable/control]' is much neater / faster than my doing an 'If' each and every pass through my loop/repeat block.

Truly - thanks a million guys. Much appreciated.

Regards.

Re: How to put the contents of an Array into a ListBox

Posted: Thu Jan 24, 2013 5:49 pm
by mwieder
Craig-

No, the OP just doesn't put a leading cr before the first entry.
It *does* put every array key into the list.
But you got the point across.

Traxgeek-

The "repeat for each" form is much faster than the "repeat with" form.

You can probably (untested right now) also make a one-liner to do the whole thing in one swell foop:

Code: Select all

  put the keys of gsPeople into button "lstPeople" of group "theGroup"

Re: How to put the contents of an Array into a ListBox

Posted: Thu Jan 24, 2013 9:31 pm
by dunbarx
Mark..

Not sure what you mean. traxGeek's script ignores the first key, and I did not change that. But generically:

repeat with y = 1 to 4
put random(99) & return after btn "combo"
delete last char of btn "combo"
end repeat

puts four numbers (menuItems) into the button. What were you saying about not putting a cr before the first entry?

Just wondering.

Craig

Re: How to put the contents of an Array into a ListBox

Posted: Thu Jan 24, 2013 11:24 pm
by mwieder
Craig-

It doesn't matter how many times you say it, it's still wrong.
Are you and I looking at the same piece of code in the first post?

Re: How to put the contents of an Array into a ListBox

Posted: Fri Jan 25, 2013 12:47 am
by dunbarx
Mark

No, I was looking at much later ones.

Never mind...

Craig

Re: How to put the contents of an Array into a ListBox

Posted: Fri Jan 25, 2013 12:59 am
by mwieder
Ah, ok, mystery solved.