lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Dave Collins once stated:
> 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)

  This also works:

local ad_lineup = 
{
  ad1 = { "CoolWhip" , "ShreddedWheat" , "Swiffer" },     
  ad2 = { "ShreddedWheat" , "Swiffer" , "Yumos" , "Concierge" , "foo" } ,      
  ad3 = { "NeedHelp" },
}

You can even drop the 'ad1' stuff ... 

local ad_lineup = 
{ 
  { "CoolWhip" , "ShreddedWheat" , "Swiffer" },     
  { "ShreddedWheat" , "Swiffer" , "Yumos" , "Concierge" , "foo" } ,      
  { "NeedHelp" },
}

This makes ad_lineup[1][2] == "ShreddedWheat"

  -spc