How to compare 2 hashes

B

Billy Patton

I'm checking the input to functions. The code below is just a snippett
The 'undef' is the problem.
It is being handled as a string.
What would be the simplest method of handling this?

use Data::Dumper;

my %legal =
(
'extendInto'=> [
{'layer'=>'\S+','comment'=>'\S+','into'=>'\S+','value'=>'\d+','coincidenceOK'=>'[01]'},
];

my %seed = (
'layer'=>'CONT','comment'=>'xxxx',=>'into'=>undef,'value'=>20,'coincidenceOK'=>1
);

if (isLegal(\@{$legal{extendInto}},\%seed)){ print "legal\n"; }
else { print "NOT legal\n"; }


sub isLegal {
my $legalHashes = shift;
my $hashUnderTest = shift;
foreach my $legalTest (@$legalHashes) {
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Useqq = 1;
my $testRegex = Dumper $legalTest;
$testRegex =~ s/\\\\/\\/g; ## replace the excaped backslashes
$testRegex =~ s/uniq\d+-//g; ## delete the uniquifier prefix
$testRegex =~ s/\"/"?/g; ## make the quotes optional as it might be
string or an integer, we treat the same
$testRegex =~ s/\'/'?/g; ## make the quotes optional as it might be
string or an integer, we treat the same

$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Useqq = 1;
my $stringUnderTest = Dumper $hashUnderTest;

print "stringUnderTest = $stringUnderTest\n";
print "testRegex = $testRegex\n";
return 1 if ($stringUnderTest =~ /$testRegex/);
}
return 0;
}

Here is the result of a run:
stringUnderTest = {"coincidenceOK" => 1,"comment" => "xxxx","into" =>
undef,"layer" => "CONT","value" => 20}
testRegex = {"?coincidenceOK"? => "?[01]"?,"?comment"? =>
"?\S+"?,"?into"? => "?\S+"?,"?layer"? => "?\S+"?,"?value"? => "?\d+"?
legal
 
B

Billy Patton

##
## remove any key whose value is undefined.
##
map { delete $hashUnderTest->{$_} unless defined $hashUnderTest->{$_}; }

keys %$hashUnderTest;
 
U

Uri Guttman

BP> ##
BP> ## remove any key whose value is undefined.
BP> ##
BP> map { delete $hashUnderTest->{$_} unless defined $hashUnderTest->{$_};
BP> }

BP> keys %$hashUnderTest;

don't use map for that. better to use the foreach modifier. map is
intended to generate new lists from old and not for side effects (like
deleting from a hash).

delete $hashUnderTest->{$_} unless defined $hashUnderTest->{$_}
for keys %$hashUnderTest;

may even be better (probably a little faster) to use a slice:

delete @{$hashUnderTest}{
grep defined $hashUnderTest->{$_}, keys %$hashUnderTest } ;

also i don't know the whole project but if this undef in a hash is a
problem, then it should be checked when the hash is made and not when it
is being processed later on.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top