How to put the contents of an Array into a ListBox

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

How to put the contents of an Array into a ListBox

Post by Traxgeek » Wed Jan 23, 2013 7:52 pm

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.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Wed Jan 23, 2013 10:25 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

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

Post by bn » Wed Jan 23, 2013 10:37 pm

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
Last edited by bn on Wed Jan 23, 2013 11:05 pm, edited 2 times in total.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

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

Post by mwieder » Wed Jan 23, 2013 10:51 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Wed Jan 23, 2013 11:41 pm

Mark.

His script also ignores the first key.

TraxGeek, is this what you wanted?

Anyway, I was after beauty, not truth.

Craig

Traxgeek
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 281
Joined: Wed Jan 09, 2013 10:11 am

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

Post by Traxgeek » Thu Jan 24, 2013 10:00 am

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.
I'm 'getting there'... just far too slowly !
Mac (Siera) and PC (Win7)
LiveCode 8.1.2 / 7.1.1

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

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

Post by mwieder » Thu Jan 24, 2013 5:49 pm

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"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Thu Jan 24, 2013 9:31 pm

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

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

Post by mwieder » Thu Jan 24, 2013 11:24 pm

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?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Jan 25, 2013 12:47 am

Mark

No, I was looking at much later ones.

Never mind...

Craig

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

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

Post by mwieder » Fri Jan 25, 2013 12:59 am

Ah, ok, mystery solved.

Post Reply