Announcement

Collapse
No announcement yet.

Lines failed from being removed (table)

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

  • Lines failed from being removed (table)

    Hi.
    I've got a code to remove lines from a file "%EarthNav%" with defined keywords. It does not seem to work although everything seems quite logical to me. Your help would be highly appreciated!




    SessionVar.Set("%EarthNav%", a_path .. "\\Custom Data\\earth_nav.dat");



    -- Remove lines from earth_nav.dat
    -- read earth_nav.dat

    mytable = TextFile.ReadToTable("%EarthNav%");
    linestable = {};
    inc = -1;



    -- keyword 1
    for line_number,line_content in pairs(mytable) do
    result = String.Find(line_content, "CARTER", 1, false); -- false means not case sensitive
    if (result > 0) then
    inc = inc + 1;
    line_number1 = line_number - inc;
    Table.Insert(linestable, 1, line_number1);
    end
    end

    -- keyword 2
    for line_number,line_content in pairs(mytable) do
    result = String.Find(line_content, "KENNEDY", 1, false); -- false means not case sensitive
    if (result > 0) then
    inc = inc + 1;
    line_number1 = line_number - inc;
    Table.Insert(linestable, 1, line_number1);
    end
    end



    -- table with line numbers which contain a line which need to be deleted
    Table.Sort(linestable, nil)
    for x,y in pairs(linestable) do
    Table.Remove(mytable, y);
    end
    -- Write earth_nav.dat
    TextFile.WriteFromTable("%EarthNav%", mytable, false);



    ...so the idea to remove all lines from the file %EarthNav% which contain "Kennedy" or "Carter".

  • #2
    Do it this way:

    Code:
    SessionVar.Set("%EarthNav%", a_path .. "\\Custom Data\\earth_nav.dat");
    
    local tWords = { "CARTER", "KENNEDY" };
    local tInFile = TextFile.ReadToTable(SessionVar.Expand("%EarthNav%"));
    local tOutFile = {};
    
    for i = 1, #tInFile do
        local bSkip = false;
        for j = 1, #tWords do
            if (String.Find(tInFile[i], tWords[j], 1, false) ~= -1) then
                bSkip = true;
                break;
            end
        end
        if not bSkip then
            Table.Insert(tOutFile, (#tOutFile + 1), tInFile[i]);
        end
    end
    
    TextFile.WriteFromTable(SessionVar.Expand("%EarthNav%") .. ".new", tOutFile, false);
    For large files, it may be better to read the file line by line and save it while being processed, instead of loading the whole amount of data into tables.

    Ulrich

    Comment


    • #3
      You are amazing! Thank you very much!

      Comment

      Working...
      X