perl - Compare Files and print difference using print unless -
i got following function , dont know how put output variable.
sub checkfiles { # declaration $origdir="/home/hbo/test/chksum/"; $tmpdir="/home/hbo/test/tmp/"; # directory inventory opendir( dir, $origdir); @files = sort ( grep { !/^\.|\.\.}$/ } readdir(dir) ); closedir(dir); foreach $file (@files) { if ( !-r $origdir.$file) { print $origdir.$file, "does not exist"; next;} # open filehandles open $a_fh, '<', $origdir.$file or die "$origdir.$file: $!"; open $b_fh, '<', $tmpdir.$file or die "$tmpdir.$file: $!"; # map difference %tmpdirfile; @tmpdirfile{map { unpack 'a*', $_ } <$b_fh>} = (); # print difference while (<$a_fh>) { print unless exists $tmpdirfile{unpack 'a*', $_}; } close $a_fh; close $b_fh; } }
the line got questions "print unless exists $tmpdirfile{unpack 'a*', $_};" want put output variable array can decide between "changed" or "deleted" or "new". short summary script do: builds md5 sum of directory, check if directory differs version before , print differences flags "new", "deleted", "changed". , yes don't want use additional libraries.
the output on console is:
40567504a8a2f9f665695a49035b381b /home/hbo/test/somedir/some/some.conf
now want show if file has changed, deleted or new. because of need put output variable. can me?
you can use hash that. it's bit counting, don't count, keep list of files in each key. this:
{ new => [ 'file1', 'file3', ], deleted => [ 'file4', ], changed => ] 'file5', ], },
create hash (or in example hash reference) outside loop, , push array ref in appropriate key. don't need create array ref, autovivification take care of you.
my $diff; # keep track here foreach $file (@files) { # ... while (my $line = <$a_fh>) { if ( exists $tmpdirfile{unpack 'a*', $line} ) { # stuff check difference if ( find_diff($line) ){ push @{ $diff->{changed} }, $line; } else { push @{ $diff->{deleted} }, $line; } } else { push @{ $diff->{new} }, $line; } } }
Comments
Post a Comment