Hi guys.
The code in question is in the end of this post.
Basically some lines need to be added to an existing file (user_nav.dat), between already existing lines and without dublicates. Lines to add are being read from a txt file (earth_nav11.txt). So the installer does this:
- reads user_nav.dat
- converts earth_nav11.txt into a table
- searches user_nav.dat for dublicated lines
- removes dublicated lines from user_nav.dat
- defines the location where new lines must be inserted
- adds new lines from earth_nav11.txt to user_nav.dat
- saves user_nav.dat
This all works fine but... new lines from earth_nav11.txt are added in the inverted order - lets say not from 1 to 9 but from 9 to 1. The order is inverted in comparison to the TXT file where the lines are taken from. Could you please suggest how to fix that? I would highly appreciate it.
-- XP11
if (SessionVar.Expand("%SimSelection%") == "605") then
-- adding new data to UserNav.dat
-- read user_nav.dat
user_nav_dat = TextFile.ReadToTable("%UserNav%");
-- read earth_nav11.txt
earth_nav11_txt = TextFile.ReadToTable("%TempFolder%\\earth_nav11.tx t");
-- setting variables
lines_table = {};
inc = -1;
-- remove any line from dat which exists in txt
-- search the dat file for any line in the txt file
for line_number_dat,line_content_dat in pairs(user_nav_dat) do
for line_number_txt,line_content_txt in pairs(earth_nav11_txt) do
-- skip empty strings
if (String.TrimRight(String.TrimLeft(line_number_dat, " "), " ") ~= "" or String.TrimRight(String.TrimLeft(line_content_txt, " "), " ") ~= "") then
-- compare the two strings
if (String.TrimRight(String.TrimLeft(line_content_dat , " "), " ") == String.TrimRight(String.TrimLeft(line_content_txt, " "), " ")) then
-- entry found, put the line number in an array so we can remove it later
inc = inc + 1;
line_number_dat_new = line_number_dat - inc;
Table.Insert(lines_table, 1, line_number_dat_new);
end
end
end
end
-- Remove the lines from user_nav.dat
Table.Sort(lines_table, nil)
for x,y in pairs(lines_table) do
Table.Remove(user_nav_dat, y);
end
-- find line number of line containing 99
for line_number,line_content in pairs(user_nav_dat) do
if (String.Replace(line_content, " ", "") == "99") then
SessionVar.Set("%line_number%", line_number)
end
end
-- Add lines from earth_nav11.txt to user_nav.dat
for line_number_txt,line_content_txt in pairs(earth_nav11_txt) do
-- skip empty lines
if (String.TrimRight(String.TrimLeft(line_content_txt , " "), " ") ~= "") then
Table.Insert(user_nav_dat, SessionVar.Expand("%line_number%"), line_content_txt);
end
end
-- Write user_nav.dat
TextFile.WriteFromTable("%UserNav%", user_nav_dat, false);
end
The code in question is in the end of this post.
Basically some lines need to be added to an existing file (user_nav.dat), between already existing lines and without dublicates. Lines to add are being read from a txt file (earth_nav11.txt). So the installer does this:
- reads user_nav.dat
- converts earth_nav11.txt into a table
- searches user_nav.dat for dublicated lines
- removes dublicated lines from user_nav.dat
- defines the location where new lines must be inserted
- adds new lines from earth_nav11.txt to user_nav.dat
- saves user_nav.dat
This all works fine but... new lines from earth_nav11.txt are added in the inverted order - lets say not from 1 to 9 but from 9 to 1. The order is inverted in comparison to the TXT file where the lines are taken from. Could you please suggest how to fix that? I would highly appreciate it.
-- XP11
if (SessionVar.Expand("%SimSelection%") == "605") then
-- adding new data to UserNav.dat
-- read user_nav.dat
user_nav_dat = TextFile.ReadToTable("%UserNav%");
-- read earth_nav11.txt
earth_nav11_txt = TextFile.ReadToTable("%TempFolder%\\earth_nav11.tx t");
-- setting variables
lines_table = {};
inc = -1;
-- remove any line from dat which exists in txt
-- search the dat file for any line in the txt file
for line_number_dat,line_content_dat in pairs(user_nav_dat) do
for line_number_txt,line_content_txt in pairs(earth_nav11_txt) do
-- skip empty strings
if (String.TrimRight(String.TrimLeft(line_number_dat, " "), " ") ~= "" or String.TrimRight(String.TrimLeft(line_content_txt, " "), " ") ~= "") then
-- compare the two strings
if (String.TrimRight(String.TrimLeft(line_content_dat , " "), " ") == String.TrimRight(String.TrimLeft(line_content_txt, " "), " ")) then
-- entry found, put the line number in an array so we can remove it later
inc = inc + 1;
line_number_dat_new = line_number_dat - inc;
Table.Insert(lines_table, 1, line_number_dat_new);
end
end
end
end
-- Remove the lines from user_nav.dat
Table.Sort(lines_table, nil)
for x,y in pairs(lines_table) do
Table.Remove(user_nav_dat, y);
end
-- find line number of line containing 99
for line_number,line_content in pairs(user_nav_dat) do
if (String.Replace(line_content, " ", "") == "99") then
SessionVar.Set("%line_number%", line_number)
end
end
-- Add lines from earth_nav11.txt to user_nav.dat
for line_number_txt,line_content_txt in pairs(earth_nav11_txt) do
-- skip empty lines
if (String.TrimRight(String.TrimLeft(line_content_txt , " "), " ") ~= "") then
Table.Insert(user_nav_dat, SessionVar.Expand("%line_number%"), line_content_txt);
end
end
-- Write user_nav.dat
TextFile.WriteFromTable("%UserNav%", user_nav_dat, false);
end
Comment