Repeat multidimensional array?

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
trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Contact:

Repeat multidimensional array?

Post by trenatos » Fri Nov 22, 2013 7:03 am

I have a simple multi-dimensional array that I'm trying to repeat over.

I'm trying to figure out how to create buttons from the array.

It's a simple array: males[number]["name"] so nothing earthshattering there, but I can't figure out how to loop over it and pull the "name" each time.
I've added to this array by using a variation of put males[1]["bob"] for testing.

Could someone help me out?
Marcus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Repeat multidimensional array?

Post by Simon » Fri Nov 22, 2013 8:23 am

Hi trenatos,
For males[number]["name"]

Code: Select all

    repeat for each key tKey in males
       answer males[ tKey ][ "name" ]
    end repeat
Fun. :D
But that is not the same for:
put males[1]["bob"]

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Contact:

Re: Repeat multidimensional array?

Post by trenatos » Fri Nov 22, 2013 9:12 am

I have a bunch of these: males[1]["bob"]
males[2]["marcus"]
males[3]["anders"]

and so on, and need to go through and grab the value of each name, but I'm not sure how to figure out how many records there are, then I believe I could just loop through the records such as

repeat counter = 1 to (the magic number)
put males[counter][2] into a variable/create a button/copy to other variable/whatever
end repeat
Marcus

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Repeat multidimensional array?

Post by Simon » Fri Nov 22, 2013 9:20 am

Then it's just:

Code: Select all

   repeat for each key tKey in males
       answer males[ tKey ]
    end repeat
The last bracket ["bob"] is not correct in your request.
errr... it says there is more info following?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Repeat multidimensional array?

Post by Klaus » Fri Nov 22, 2013 12:59 pm

Hi trenatos,
trenatos wrote:I have a bunch of these: males[1]["bob"]
males[2]["marcus"]
males[3]["anders"]

and so on, and need to go through and grab the value of each name, but I'm not sure how to figure out how many records there are, then I believe I could just loop through the records such as

repeat counter = 1 to (the magic number)
put males[counter][2] into a variable/create a button/copy to other variable/whatever
end repeat
do this to retrieve your "magic number:

Code: Select all

...
pu the keys of male into tMaleKeys
put the num of lines of tMaleKey into theMagicNumber
repeat with counter = 1 to theMagicNumber
...
Best

Klaus

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Contact:

Re: Repeat multidimensional array?

Post by trenatos » Fri Nov 22, 2013 6:22 pm

Klaus, I haven't tested the code yet, but that looks exactly like what I need.
I knew it'd be something simple, but not able to figure it out.

Thank you!
Marcus

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Repeat multidimensional array?

Post by Klaus » Fri Nov 22, 2013 6:29 pm

Hi trenatos,

my pleasure :D

But if you want SPEED, use Simon's suggestion:
...
repeat for each key tKey in male
## do something with: male[tKey]
end repeat
...
If applicable in your case.


Best

Klaus

Post Reply