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

I am comparing lists of invalid database objects, and trying to determine when values in the second list don't match the values in the first list. (I have to warn the user, via a Jenkins build, when the list has grown and/or new values have appeared)

The exists block fails at runtime, but I cannot figure out a clean, easy way to traverse the 'postbuilds' array to check if the value exists. Any help/pointers would be GREATLY appreciated!

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array1 = ( "proc1", "proc2", "proc3", "proc4" ); my @array2 = ( "proc1", "proc2", "proc3", "proc4", "proc5" ); my %HoA = ( prebuilds => [@array1], postbuilds => [@array2], ); print Dumper \%HoA; while ( my ( $key, $reference ) = each %HoA ) { foreach my $temp ( @{$reference} ) { print "key: $key, temp: $temp\n"; # look to see if a value in the postbuild list # exists in the prebuild list if ( exists( $HoA{'postbuild'}->$temp ) ) { print "yes\n"; } else { print "no\n"; } } }

Thank you!

Replies are listed 'Best First'.
Re: Comparing values within a Hash of Arrays
by stevieb (Canon) on Apr 24, 2012 at 14:22 UTC

    You can't use exists in this case, as you are not looking to see if a hash key exists, you need to check within the array to see if an element exists. Here's an example of what I think you want to achieve:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use List::Util qw( first ); my @array1 = ( "proc1", "proc2", "proc3", "proc4" ); my @array2 = ( "proc1", "proc2", "proc3", "proc4", "proc5" ); my %HoA = ( prebuilds => [@array1], postbuilds => [@array2], ); for my $postbuild ( @{ $HoA{ postbuilds } } ){ print "$postbuild: "; if ( first { $_ =~ $postbuild } @{ $HoA{ prebuilds } } ){ print "yes\n"; } else { print "no\n"; } }

      Ha! Just saw your edit - did the same, because your direction/suggestion was spot on! Thank you to everyone for replying!

        I just edit it back :) Let me know which way you needed it originally and I'll fix it permanently

Re: Comparing values within a Hash of Arrays
by Anonymous Monk on Apr 24, 2012 at 14:17 UTC

    Like this

    sub Fompare { my( $pree, $post ) = @_; for my $item ( @{ $pree } ){ print "$item\n"; print "yes\n" if InIt( $item, $post ); } } sub InIt { my( $item, $post ) = @_; for my $postItem ( @$post ){ return !!1 if $item eq $postItem; return !!1 if $item == $postItem; } }

    But see references quick reference and grep

Re: Comparing values within a Hash of Arrays
by tommyxd (Acolyte) on Apr 24, 2012 at 14:18 UTC

    If the value you're looking for does not exist in postbuilds its value should be undef, which is evaluated as false in a conditional expression. Therefor I think you could just do:

    if ($HoA{'postbuild'}->$temp) { print "yes\n"; }

    Hope this helps!