in reply to Print both elements in a compare
Try putting them together something like:my %hash; for my $key (@array2){ $hash{$key}++; } for my $key (@array1){ print "Fail: %key \n" unless $hash{$key}; }
Two notes about that however. The first is that you don't know from this what the key is in array 1 that has caused the error, you just know which key mismatches. Second, if you have an array of numbers and one of them is 0 you'd return a false value for that grep. What I'd really recommend is a destructive process on the arrays. If you really need to keep those two alive then you can copy them.for my $key (@array2){ $hash{$key}++ print "Fail $key" unless grep($key, @array1); }
The one note about the above code is that if array1 and array2 are not the same length you're gonna have a fatal error and die.my $hash; while (@array1){ my $var1 = shift @array1; my $var2 = shift @array2; if($var1 == $var2){ $hash{$var1}++; } else { print "Key mismatch array1 value $var1 != $var2 (array2 value)"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Print both elements in a compare
by Anonymous Monk on Nov 01, 2010 at 19:16 UTC | |
by Marshall (Canon) on Nov 01, 2010 at 20:07 UTC |