in reply to Comparing the filepaths and versions in two hashes

I'm not sure what you really want in terms of output. What sort of question(s) are you trying to answer regarding the two input lists?

What should happen if a single "path/filename" appears multiple times with different version numbers in the first list, and then in the second list: (a) it never occurs, or (b) it occurs but only with different version numbers, or (c) it occurs with some or all version numbers matching the first list?

In order to compare the two lists on the basis of matches in the full path/name string, it would be easiest to use the path/name string as a primary hash key (and probably use the version numbers as secondary hash keys -- i.e. an HoH structure, rather than HoAoH),

This is different enough from your previous question (Constructing a hash with filepath,filename and filerev) that you probably don't want to start from an exact copy of that code to do this task. Here's how I would start out, but I wouldn't know how to finish it until you clarify what you really want.

#!/usr/bin/perl use strict; use warnings; @ARGV == 2 and -f $ARGV[0] or die "Usage: $0 list1 list2\n"; my ( $list1, $list2 ) = @ARGV; my %pathname_versions; open( IN, $list1 ) or die "$list1: $!"; while (<IN>) { next unless ( m:(.+?)#(\d+)$: ); $pathname_versions{$1}{$2} = undef; } open( IN, $list2 ) or die "$list2: $!"; while (<IN>) { next unless ( m:(.+?)#(\d+)$: ); if ( exists( $pathname_versions{$1} )) { # we have a match on the path/name... now what? if ( exists( $pathname_versions{$1}{$2} )) { # we have a match on version# as well, so... } else { # version number wasn't seen in list1, so... } } }
BTW, you really should learn to use @ARGV and command-line args -- it simplifies a lot of things when you actually use the script, as well as when you write it.

Replies are listed 'Best First'.
Re^2: Comparing the filepaths and versions in two hashes
by perl_mystery (Beadle) on Dec 11, 2010 at 21:49 UTC

    INPUT: List1: //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

    Eg.//depot/asic/tools/perl/files/examples/fpower.cpp#2

    List2: //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

    Eg.//depot/asic/tools/perl/files/examples/fpower.cpp#2

    while (<IN>) { next unless ( m:(.+?)#(\d+)$: ); if ( exists( $pathname_versions{$1} )) { #pathname exists in list1 # compare the versions of the matching pathnames ,if the versi +on is lower in list2 print the #pathnames with both versions.Eg //depot/asic/tools/perl/scripts/e +xamples/modem.c#7-> 6. #Basically this block should find all the lower version pathnames +in list2 compared to list1 if ( exists( $pathname_versions{$1}{$2} )) { # we have a match on version# as well, so... #just print "duplicate file found" with the pathname and versi +on #Eg.//depot/asic/tools/perl/files/examples/fpower.cpp#2 } else { # version number wasn't seen in list1, so... #this case hardly araises,there will always be a version# asso +ciated #we can just print "No version number" } } else { #working on logic what if the pathnames doesn't match } }
      <a href="?node_id=174051">How do I post a question effectively?</a> says not to hide your question in code tags, or code comments

      1.What's wrong with the below code.I am getting the below error

      syntax error at orphan_overwrite.pl line 25, near "}{" syntax error at orphan_overwrite.pl line 35, near "}" Execution of orphan_overwrite.pl aborted due to compilation errors.

      2.Is the if condition"if ($2 < pathname_versions{$1}{$2} )" valid?Does pathname_versions{$1}{$2} give the version value in List1 ?How do I access the value for the matched path in list1 and compare?

      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; @ARGV == 2 and -f $ARGV[0] or die "Usage: $0 list1 list2\n"; my ( $list1, $list2 ) = @ARGV; my %pathname_versions; open( IN, $list1 ) or die "$list1: $!"; while (<IN>) { next unless ( m:(.+?)#(\d+)$: ); $pathname_versions{$1}{$2} = undef; } print Dumper( \%pathname_versions ); open( IN, $list2 ) or die "$list2: $!"; while (<IN>) { next unless ( m:(.+?)#(\d+)$: ); #print "\nLIST 2:$1 $2"; if ( exists( $pathname_versions{$1} )) { if ($2 < pathname_versions{$1}{$2} ) {#if the version in list2 + is less than version for the same path in list1 print "$1 pathname_versions{$1}{2}->$2"; } if ( exists( $pathname_versions{$1}{$2} )) { # we have a match on version# as well, so... } else { # version number wasn't seen in list1, so... } } }

        Details matter.  In

        pathname_versions{$1}{$2}

        you omitted the '$' sigil.