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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.