A Question About LUA, Please

Peer support for Air Manager desktop users

Moderators: russ, Ralph

Message
Author
The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

A Question About LUA, Please

#1 Post by The Artful Dodger »

Hello:

Has anyone had any experience with multi-dimensional arrays in LUA? For example:

Code: Select all

A = {{},{}}		-- Defines a two-dimensional array A.
A[1][1] = 3, A[1][2] = 5; A[1][3] = A[1][1] - A[1][2]; A[1][4] = "25"
A[2][1] = 5, A[2][2] = 6; A[2][3] = A[2][1] - A[2][2]; A[2][4] = "45"
-- All of the above works fine.
A[3][1] = 9, A[3][2] = 2; A[3][3] = A[3][1] - A[3][2]; A[3][4] = "45"
-- As soon as I get above A[2][n], I get an error.
I can find nothing, and I've looked in two LUA textbooks, that describes first, how to define a multi-dimensional array (I had to figure that out), and then nothing describes why it would stop at 2.
Any help would be most appreciated.

Sparky

The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Re: A Question About LUA, Please

#2 Post by The Artful Dodger »

Hello, again:
I have found a third book which alludes to multi-dimensional arrays without mentioning them as such. So, here's what I found, if you ever need one:

Code: Select all

--
A = {{}, {}, {}, {}, {}, {}, {}}   -- an array A[1][n] to A[7][m] where the max size of the first dimension is the number of {} you use!!
-- Set and refer to a value thus:
A[3][4] = 4
--
This is handy when you have a sequence of things. Suppose you do different things dependent upon hundreds of knots where 0 - 99 goes in A[1], 100 - 199 goes in A[2], etc. Then you can refer to A[AS//100] and you can thus refer to everything you've set up for 0 - 99 knots or 100 - 199, etc. Pretty nifty!
If all of this is "old" to anyone, I'm sorry to repeat it, but using multi-dimensional arrays in LUA is not done very much.

Sparky

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: A Question About LUA, Please

#3 Post by jph »

Search google for lua mulidimentional arrays and tables and metatables.
Joe. CISSP, MSc.

The Artful Dodger
Posts: 204
Joined: Sat Jul 09, 2022 3:20 pm

Re: A Question About LUA, Please

#4 Post by The Artful Dodger »

That's exactly what I did and found a tutorial on it. But thanks.

Sparky

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: A Question About LUA, Please

#5 Post by jph »

Ah, you posted as I was posting.. glad you found some info, it is not a direct comparison with lua, tables are incredibly powerful though.
Joe. CISSP, MSc.

User avatar
Sling
Posts: 5237
Joined: Mon Sep 11, 2017 2:37 pm
Contact:

Re: A Question About LUA, Please

#6 Post by Sling »

Use them all the time. Useful for storing and retrieving groups of data.

JackZ
Posts: 2262
Joined: Mon Feb 22, 2016 1:02 pm

Re: A Question About LUA, Please

#7 Post by JackZ »

A multidimensional array in Lua is basically an array of arrays (or tables which is the proper name in Lua).
It can be easier to create it as a two step process:
you have to declare an array as usual, say you wanted a 2D array (can be more) like

Code: Select all

position [line] [column]
- First declare a single dimension table

Code: Select all

position = {} — create a 1 dimension array 
- then create another array for each 1st dimension

Code: Select all

maxcolumn=255
for i=1, maxcolumn do
     position[i]={}
end
Then you can fill the array using both indexes.

Code: Select all

column=130
line=50
position[column][line]=“C”
And retrieve that value later on.

Code: Select all

value=position[130][50]
print(value)
My YouTube Chanel on the A320 (Real SOPs by an Airline Pilot IRL):
https://www.youtube.com/playlist?list=P ... 0Q6SBASRqJ

User avatar
jph
Posts: 2846
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: A Question About LUA, Please

#8 Post by jph »

@JackZ
Jacques, that is probably the best and clearest example I have seen ;)
Nicely presented.
Joe
Joe. CISSP, MSc.

User avatar
Ralph
Posts: 7878
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: A Question About LUA, Please

#9 Post by Ralph »

Indeed nice. It was probably for the demonstration factor, but I believe that you can expand it along the way, I don't think that you have to create it first. But I might be wrong.

Tetrachromat
Posts: 236
Joined: Sun Feb 14, 2021 6:55 pm

Re: A Question About LUA, Please

#10 Post by Tetrachromat »

The Artful Dodger wrote: Thu Sep 29, 2022 5:17 pm Hello:

Has anyone had any experience with multi-dimensional arrays in LUA? For example:

Code: Select all

A = {{},{}}		-- Defines a two-dimensional array A.
A[1][1] = 3, A[1][2] = 5; A[1][3] = A[1][1] - A[1][2]; A[1][4] = "25"
A[2][1] = 5, A[2][2] = 6; A[2][3] = A[2][1] - A[2][2]; A[2][4] = "45"
-- All of the above works fine.
A[3][1] = 9, A[3][2] = 2; A[3][3] = A[3][1] - A[3][2]; A[3][4] = "45"
-- As soon as I get above A[2][n], I get an error.
I can find nothing, and I've looked in two LUA textbooks, that describes first, how to define a multi-dimensional array (I had to figure that out), and then nothing describes why it would stop at 2.
Any help would be most appreciated.

Sparky
The initial question was, why on the fourth line a failure occurs.

The issue is that with

Code: Select all

A[3][1] = 9
you are indexing a NIL value. A[3] is not defined at that point so LUA evaluates this to NIL. And indexing a NIL value is not allowed.

Correct (in your style), would be:

Code: Select all

A[3] = {}
A[3][1] = 9, A[3][2] = 2; A[3][3] = A[3][1] - A[3][2]; A[3][4] = "45"
Paul

Post Reply