lua-users home
lua-l archive

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

On Thursday 22 April 2004 08:06, wrote:
> <html><style>p {margin: 0px}</style><body bgcolor='#ffffff'
> style='font-size:9pt; font-family:Verdana; font-family: Verdana' ><P>Hello,
> hope there's somebody out there who can help me.</P><P>I tried to make a
> 2-dimensional array for the last 2 days. And doesn't work.</P><P>Ich wamnnt
> to have an array with 2 indices ([i] and [n]), both indices are
> count-variables in a for-loop!</P><P>Programm should for example work like
> this:</P><P>for i = 0,3 do</P><P>for n = 0,20 do</P><P>array[i][n] = X -- X
> is a value, I get from an analyse, it is dependent from i and n
> </P><P>end</P><P>end</P><P>-- so that I can get results like array[1][5] =
> 456 and write them to a file in another loop</P><P>by using
> </P><P>write(handle,array[i][n],"\n")</P><P>I always have problems with
> "nil value"</P><P>Hope somebody can help me! Thanks</P><br><br><table
> cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="#000000"><img
> src=""; width="1" height="1" border="0" alt="" /></t!
> d></tr><tr><td style="font-family:verdana; font-size:12px;
> line-height:17px;">Der WEB.DE Virenschutz schuetzt Ihr Postfach vor dem
> Wurm Netsky.A-P!&nbsp;&nbsp;<br>Kostenfrei fuer alle FreeMail Nutzer. <A
> HREF="";><B></B></A>&nbs

Please don't post to mailing lists in HTML!

In answer to your question, the problem is that a multidimensional array is 
just an array of arrays, so you need to create all the tables which represent 
each row. I'd suggest using a function like this (untested):

function make_array(...)
    local n = arg[1]
    local r = { n = n }
    if arg.n == 1 then return r end
    table.remove(arg, 1)
    for i = 1,n do r[i] = make_array(unpack(arg)) end
    return r

Then you can create, e.g. a 4x5x7 array like so:

a = make_array(4, 5, 7)

-- Jamie Webb