PitifulProgrammer has asked for the wisdom of the Perl Monks concerning the following question:
first off, happy new year to all of you.
What better way to start a new year than with a perl question. I am still a noob, not having had many chances to dig deeper into coding in the last months.
The issue is as follows:
After running a piece of code that finds particular files and replaces some characters if need be, the output folder contains the original files and backup files marked by .bak.
Previously, it was sufficient to compare the files manually using a diff tool. I also used the UNIX diff if there were more than a few files to check.
Recently, the number of folders and files has increased and I would like the comparison to be script-based, too.
The trouble is that I cannot find a starting point. I have been toying with modules such as Array::Diff and Array::Compare, but the output was always a hash reference not a report ( at least I think it was a hash reference ).
I also tried out Text::Diff leading to the same result
To put it in a nutshell: Given two arrays in a given folder, e.g.:
my @xml_files = glob( '*xml' ); #say for @xml_files; my @bak_files = glob( '*bak' ); #say for @bak_files; __END__ These would be the files: file_01.xml file_02.xml file_03.xml file_01.xml.bak file_02.xml.bak file_03.xml.bak
I would like to compare file_01.xml with file_01.xml.bak and so on. I was considering of maybe creating a hash but refrained from doing so since, as far as I have been told, hash items are unordered.
I am even not sure by now if separating the files into 2 arrays is a wise move.
Could somebody please give me a hint about how to approach this problem?
Please find my endeavours below, some of which have already been posted here in the forum and I would also like to thank those people. I am just adding these snippets, just in case I missed a convenient approach already.
Personally, the main difficulty is to think of a solution how to make sure that file_01.xml is compared with file_01.xml.bak and the moving on to the next and selecting the right data structure for doing so.
Thanks in advance for your suggestions
Kind regards
C.use 5.018; use strict; use warnings; use Data::Dumper; use File::Glob; use List::Compare; use Array::Diff; use Array::Compare; #Separating xml and backup files my @xml_files = glob( '*xml' ); #say for @xml_files; my @bak_files = glob( '*bak' ); #say for @bak_files; #Show differences between file_01.xml and file_01.xml.bak, etc... my $diff_arrays = Array::Diff -> diff( \@xml_files, \@bak_files ); my $count = $diff_arrays -> count; my $added = $diff_arrays -> added; my $deleted = $diff_arrays -> deleted; #say $deleted; #Doing the same thing with Array::Compare my $compared = Array::Compare->new(DefFull => 1); my $differences = $compared -> full_compare(\@xml_files, \@bak_files); + # Full comparison say for $differences; __END__ my $are_equal = compare_arrays( \@xml_files, \@bak_files ); sub compare_arrays{ my( $first, $second ) = @_; # any array used by code or cmd return 0 unless @$first == @$second; for ( my $i = 0, $i < $first, $i++ ){ return 0 if $first -> [$i] ne $second -> [$i]; } return 1; } ###################################################################### +## if (compare ( glob( *bak, *xml) ) == 0) { print "They're equal\n"; } ###################################################################### +### foreach my $file( @files ){ if (compare ( glob(*.bak, *.xml) ) == 0) { print "They're equal\n"; } } my @files = $ARGV[0]; ###################################################################### +### foreach my $element( @xml_files ){ if ( $element ~~ $bak_files[$counter] ){ say "equal!!!"; } else { say "not equal!!!"; say $element; } $counter++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: comparing contents of two arrays and output differences
by RichardK (Parson) on Jan 02, 2015 at 11:27 UTC | |
by PitifulProgrammer (Acolyte) on Jan 02, 2015 at 12:14 UTC | |
by RichardK (Parson) on Jan 02, 2015 at 13:57 UTC | |
by PitifulProgrammer (Acolyte) on Jan 02, 2015 at 14:22 UTC | |
by roboticus (Chancellor) on Jan 02, 2015 at 15:55 UTC | |
| |
by 2teez (Vicar) on Jan 02, 2015 at 15:58 UTC | |
| |
|
Re: comparing contents of two arrays and output differences
by Anonymous Monk on Jan 02, 2015 at 11:07 UTC |