More Lua adventures.
So I have a waypoint autopilot that takes lat/long data from the Orbiter Cities.mkr file and converts it to a table of tables that is searchable by index, so the user can scroll through the cities and select the desired waypoint.
The resulting table has a number of tables in it that is the same as the number of cities (n_cities) listed in the Cities.mkr file. As an example, it looks something like this (not exact numbers, just making up an example):
This works fine, but there are hundreds of cities, and they are unfortunately not in alphabetical order, so if you want to fly to a certain city, you may need to scroll through and examine a large number of waypoint tables before finding the city you desire. So I am trying to sort the tables by their city name using the following code at the start of the simulation. It basically looks at the city names in index j and index j+1 and sorts them. If the index of the sorted name table and the original don't match, a swap occurred, so the the waypoint tables are also swapped. This is run n_cities times to ensure that the table is fully sorted.
This code runs, but it doesn't seem to do anything. The city_waypoints table is unmodified. I have verified that the temp_pair table is pulling the names correctly. Table.sort requires integer keys for the table to be sorted, and they seem to be. As far as I can tell this code is correct and valid, but it doesn't do anything.
This seems to be a strictly Lua question, but I wonder if I am using too modern Lua syntax for the Orbiter interpreter. But that usually causes a crash. I'm also wondering a little bit about the indices/keys for these tables. I've tried to explicitly set integer keys and it doesn't change anything.
Any thoughts?
So I have a waypoint autopilot that takes lat/long data from the Orbiter Cities.mkr file and converts it to a table of tables that is searchable by index, so the user can scroll through the cities and select the desired waypoint.
The resulting table has a number of tables in it that is the same as the number of cities (n_cities) listed in the Cities.mkr file. As an example, it looks something like this (not exact numbers, just making up an example):
Code:
city_waypoints = {
{lat = 0.4854, lng = -2.5859, city = La Paz},
{lat = 0.9834, lng = +1.5859, city = Boston},
{lat = 0.7754, lng = -3.5859, city = Reno},
.
.
.
}
This works fine, but there are hundreds of cities, and they are unfortunately not in alphabetical order, so if you want to fly to a certain city, you may need to scroll through and examine a large number of waypoint tables before finding the city you desire. So I am trying to sort the tables by their city name using the following code at the start of the simulation. It basically looks at the city names in index j and index j+1 and sorts them. If the index of the sorted name table and the original don't match, a swap occurred, so the the waypoint tables are also swapped. This is run n_cities times to ensure that the table is fully sorted.
Code:
temp_pair = {}
temp_sort = {}
for i=1,n_cities do
for j=1,n_cities-1 do
temp_pair = {city_waypoints[j].city, city_waypoints[j+1].city} --make a sub-table with the city names from waypoint tables j and j+1
temp_sort = temp_pair --store the original
table.sort(temp_pair) --this directly modifies temp_pair
if temp_pair[1] ~= temp_sort[1] then --compare the first index entries of both tables, if different, swap the city table entries
city_waypoints[j], city_waypoints[j+1] = city_waypoints[j+1], city_waypoints[j]
end
end
end
This code runs, but it doesn't seem to do anything. The city_waypoints table is unmodified. I have verified that the temp_pair table is pulling the names correctly. Table.sort requires integer keys for the table to be sorted, and they seem to be. As far as I can tell this code is correct and valid, but it doesn't do anything.
This seems to be a strictly Lua question, but I wonder if I am using too modern Lua syntax for the Orbiter interpreter. But that usually causes a crash. I'm also wondering a little bit about the indices/keys for these tables. I've tried to explicitly set integer keys and it doesn't change anything.
Any thoughts?