Announcement

Collapse
No announcement yet.

Lines added in inverted order

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stanislaw
    replied
    Works perfectly, as always. Thank you very much!

    Leave a comment:


  • Ulrich
    replied
    Try this:

    Code:
    -- Add lines from earth_nav11.txt to user_nav.dat
    local target_line = SessionVar.Expand("%line_number%");
    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, target_line, line_content_txt);
            target_line = target_line + 1;
        end
    end
    Ulrich

    Leave a comment:


  • Stanislaw
    replied
    OK, I guess I get it... So all lines are inserted into the same %line_number% location which makes them inserted in a wrong order. So the question is - how do I increase the value during that process?

    Leave a comment:


  • Stanislaw
    replied
    Thanks Ulrich, it must be just as you say, but is there a quick way to fix this in the code?

    Actually, as far as I understand (which might obviously be wrong as this part of the code is not mine) the %line_number% is used to define a location where the code should be inserted (above the line containing "99" which is the bottom line of the DAT file) so it should work just like copy/paste. I mean - the whole content of the TXT file should be inserted there, without edits. I thought that the whole TextFile.ReadToTable procedure is used only to find and remove dublicated lines from the DAT file prior to copying the content of the TXT file into it... hence I still have no idea why the lines are inserted in a reversed order... Sorry I know this is like 2+2 for you

    Leave a comment:


  • Ulrich
    replied
    You are inserting the lines always at position %line_number%, instead of increasing the value, to maintain the order.

    Ulrich

    Leave a comment:


  • Stanislaw
    started a topic Lines added in inverted order

    Lines added in inverted order

    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
Working...
X
😀
🥰
🤢
😎
😡
👍
👎