INPUT: File1: //depot/asic/tools/perl/scripts/examples/modem.c#7
//depot/asic/tools/perl/files/examples/file.txt#2
//depot/asic/tools/perl/proc/examples/apps.c#14
File2: //depot/asic/tools/perl/scripts/examples/modem.c#6
//depot/asic/tools/perl/files/examples/file.txt#2
//depot/asic/tools/perl/proc/examples/apps.c#12
OUTPUT: Picking File2 overwrites
//depot/asic/tools/perl/scripts/examples/modem.c#7-> 6
//depot/asic/tools/perl/proc/examples/apps.c#14->12
#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; my (%files1_by,%files2_by); print "Enter File1 "; my $file1_name = <>; chomp($file1_name); open my $DATA, '<', $file1_name or die "Cannot open file 1\n"; print "Enter File2 "; my $file2_name = <>; chomp($file2_name); open my $LINE, '<', $file2_name or die "Cannot open file 1\n"; #Construct hash for file1 while (<$DATA>) { my ($file_path, $file_name, $version) = m{^(.*/(.*))#(\d+)$}; push @{ $files1_by{$file_name} }, { file_path => $file_path, version => $version, }; } close $DATA; #Construct hash for file2 while (<$LINE>) { my ($file_path, $file_name, $version) = m{^(.*/(.*))#(\d+)$}; push @{ $files2_by{$file_name} }, { file_path => $file_path, version => $version, }; } close $LINE; open my $hash_file1, '>', "hash_file1.txt"; print $hash_file1 Dumper( \%files1_by ); open my $hash_file2, '>', "hash_file2.txt"; print $hash_file2 Dumper( \%files2_by ); for my $file_name (sort keys %files2_by) { for my $href (@{ $files2_by{$file_name} }) { #print "File name: $file_name\n"; #print "File path: $href->{file_path}\n"; #print "File version: $href->{version}\n"; grep $href->{file_path} #Having trouble writing this piece of code #get the file_path in files2_by and then find/grep the files2 path in +the files1_by path #if the filepaths matches compare the version numbers and then print t +he output as shown above push @{ $hash_match{ $file_name } }, grep( (/\/\Q$href->{file_path}\E# +/i), @{ $files1_by{ $href->{file_path} } }); } }
In reply to Comparing the filepaths and versions in two hashes by perl_mystery
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |