blackgoat has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: comparison of two hashes
by BioLion (Curate) on Feb 02, 2010 at 09:08 UTC

    Hello, you are going to have to give us a bit more information if we are going to be able to help you:

    • What have you tried? Code? Data? Error messages ( Always use strict and warnings )?
    • What do your hashes look like (try printing them out with Data::Dumper) or what do yout *think/want* they/them to look like? The way you have written them out gives us no clue as to how they are structured! You can use < code >< \code > tags to preserve formatting (indentation etc... see Markup in the Monastery) if you prefer to write it out that way.
    • How do you want to compare them? Keys? Values? Both? Memory location? Structural equivalence? etc...
    • Maybe check out Data::Compare...
    Hope this helps, but in future try to make sure that questions (and answers) you post follow the guidelines given in How do I post a question effectively?.

    Just a something something...
      That's the point, i cant seem to figure out how should i structure them. i dont know too well how to use hashes of hashes. i want to write code which reports all the differences between the two libraries. the 1st hash should be iterated and check its existence in hash 2 and then vice versa.
Re: comparison of two hashes
by toolic (Bishop) on Feb 02, 2010 at 18:48 UTC
    One way is to read each file into it's own HASHES OF ARRAYS data structure, then use Data::Comparator and Data::Dumper to display the differences between them:
    use strict; use warnings; use Data::Comparator qw(data_comparator); use Data::Dumper; my $file1 = <<EOF; Cell: AND1 A1 A2 Y Cell: INV1 A Z EOF my $file2 = <<EOF; Cell: AND1 A B Y Cell: INV2 A Z EOF my $fh; my $cell; my %lib1; open $fh, '<', \$file1 or die "Can not open file: $!"; while (<$fh>) { chomp; if (/ ^ Cell: \s+ (.*) /x) { $cell = $1; } else { my @ports = split; push @{ $lib1{$cell} }, \@ports; } } my %lib2; open $fh, '<', \$file2 or die "Can not open file: $!"; while (<$fh>) { chomp; if (/ ^ Cell: \s+ (.*) /x) { $cell = $1; } else { my @ports = split; push @{ $lib2{$cell} }, \@ports; } } my $diff = data_comparator(\%lib1, \%lib2); print Dumper($diff) unless ($diff->is_empty()); __END__ $VAR1 = bless( { 'AND1' => bless( [ bless( [ bless( do{\(my $o = 'A')} +, 'Data::Differences' ), bless( do{\(my $o = 'B')} +, 'Data::Differences' ) ], +'Data::Differences' ) ], 'Data::Differences' ), 'INV2' => bless( do{\(my $o = [ [ 'A', 'Z' ] ])}, 'Data::Differences' ) }, 'Data::Differences' );
      Thanks a BUNCH! This helps a lot! Just one more small help from you, i am not sure i fully understand what this portion does:
      my $file1 = <<EOF; Cell: AND1 A1 A2 Y Cell: INV1 A Z EOF my $file2 = <<EOF; Cell: AND1 A B Y Cell: INV2 A Z EOF
      forgive me if its a stupid question! Thanks again!
        The <<EOF syntax is known as a "here-doc", which you can read about in Quote and Quote like Operators.

        I use it here to assign a multi-line string to a scalar variable. This was a convenient way for me to create a completely self-contained example which you could just copy-n-paste and run, without having to create 2 extra, external data files. Of course, in your application, you would read your 2 input files into your script.