Quantcast
Channel: Indigo Rose Software Forums - Forums
Viewing all articles
Browse latest Browse all 2105

Compare files line by line

$
0
0
Hi.
For some reason tables seem to be out of my mind's reach... so I need to ask for your help again...

Basically there are 2 files - one "default" and one with custom data. Data should be copied into the "default" file but only in case it is not already copied (so only one copy should exist in the "default" file). I had a piece of code for that but it does not seem to work:

Code:
-- 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.txt");
-- 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
user_nav.dat - this is the "default" file
earth_nav11.txt - this is the file with custom data

The whole code for implementing the custom data into the "default" file works good but only unless there is no "conflicting" lines detected. If they are, data is messed up and few lines from the beginning of the "default" file are removed which makes the file useless.

As far as I understand, both files' content is converted into a table and compared line by line. The idea is to remove from the "default" file ( user_nav.dat ) all lines which exist in the earth_nav11.txt ... Not sure if this description is clear at all...

Viewing all articles
Browse latest Browse all 2105

Trending Articles