in reply to Re: comparing contents of two arrays and output differences
in thread comparing contents of two arrays and output differences

Dear RichardK

Thanks a mil for your reply. To be honest I have not yet considered the possibility of checking if there was a backup of the files ( since I trust the script :) ). I will surely implement your suggestion. It complements the code nicely, espcially if one considers that I won't be the one using the script

I am however more interested in what was changed in each of the files ( if changes were made ), hence the reference to diff utilities.

I am sorry if the description of my problem might have been a misleading or not that well written

Thanks a mil again for your input

Kind regards

C.
  • Comment on Re^2: comparing contents of two arrays and output differences

Replies are listed 'Best First'.
Re^3: comparing contents of two arrays and output differences
by RichardK (Parson) on Jan 02, 2015 at 13:57 UTC

    The code you posted only compares the filenames not the content of the files, hence my confusion.

    why not shell out to `diff` if that's giving you the results you need? or try something from cpan like Text::Diff ?

      Dear RichardK

      Thanks a mil for pointing that out. I guess I was confused when tackling the problem. Explains why I could not figure it out

      I will check out that module ( I might have already done so, but I'll give a second go ). Just out of curiosity: The approach would then be to open each file in the one array, open the corresponding backup file in the second array and then compare the contents. After that close both files and do the same thing with the next file until all files have been compared?

      I am indeed using the shell, but I guess future users will not be familiar with the shell, that's why I was looking for a more or less "built-in" solution

      I'll post my results once I had a chance of getting back to the code

      Thanks again for providing a new perspective.

      Kind regards

      C.

        PitifulProgrammer:

        When Richard_K mentioned "shell out to `diff`" he didn't mean for the user to use diff manually, but for your program to do the work of creating the command line and running it for the user to get the desired results. Consider this:

        open my $FH, '>', "file_difference_report" or die $!; my @base_file_names = ( 'file1', 'file2', 'file3', 'file4' ); for my $file_name (@base_file_names) { if (! -e "$file_name.xml") { print "$file_name.xml: Not present ... not interesting file?\n"; next; } if (! -e "$file_name.bak") { print "$file_name: no backup, so probably not changed\n"; next; } # If we get here, we have a .bak and a .xml file, so make another +program # compare them for us: my $output = `diff $file_name.xml $file_name.bak`; print $FH "\n\n===== $file_name changes =====\n"; print $FH $output; print $FH "\n\n"; }

        In the line starting "my $output", we shelled out to use the diff command to compare the files and store the result in $output. From there you can do what you want with the results, such as concatenate it to the end of a report, as done here.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        Or you could use a subset of Text::Diff called Text::Diff::Table. Such that you print out the difference in a table like using diff -y text1 text2 in *ux OS, just like RichardK mentioned previously.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me