[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: double dim'd arrays
- From: Rob Kendrick <rjek@...>
- Date: Fri, 15 Jul 2011 15:00:40 +0100
On Fri, Jul 15, 2011 at 09:51:15AM -0400, Dave Collins wrote:
> I've got an array of rotating adverts, each one has a lineup of ads it will go through. So I made a double-dimensioned array.
>
> So:
>
> local ad_lineup ={}
> ad_lineup.ad1 ={} -- ad 1 lineup (*)
> ad_lineup.ad1[1] = "CoolWhip"
> ad_lineup.ad1[2] = "ShreddedWheat"
> ad_lineup.ad1[3] = "Swiffer"
>
> ad_lineup.ad2 ={} -- ad 2 lineup (*)
> ad_lineup.ad2[1] = "ShreddedWheat"
> ad_lineup.ad2[2] = "Swiffer"
> ad_lineup.ad2[3] = "Yumos"
> ad_lineup.ad2[4] = "Concierge"
> ad_lineup.ad2[5] = "foo"
>
> ad_lineup.ad3 ={} -- ad 3 lineup (*)
> ad_lineup.ad3[1] = "NeedHelp"
>
> Something tells me there is a more appropriate way to do this. In particular, do I need to declare each ad lineup "column" as a table in its own right? (see (*) lines above)
Nothing is stopping you putting tables into tables: you can put any
value in a table as key or value except nil. So, perhaps:
ad_lineup = {
{ "CoolWhip", "ShreddedWheat", "Swiffer" },
{ "ShreddedWheat", "Swiffer", "Yumos", "Concierge", "foo" },
{ "NeedHelp" }
}
ad_lineup[1][2] is "ShreddedWheat", and add_lineup[2][3] is "Yumos".
You can also add them after the fact:
ad_lineup[4] = { "Spatula", "Plinth", "Boing" }
Or a variation on how you're doing it at the moment:
ad_lineup[4] = { }
ad_lineup[4][1] = "Tentacle"
ad_lineup[4][2] = "Spoo"
etc. Or, you could create some functions that create your data
structure for you, so you could express it like this:
AddToLineup(2, "Swiffer") or similar. Lots of options.
B.