You seem (in another post under this thread) to want more than just intersection
and equality. There is no real efficient way to do this under any language, but
if you want to report over/under relationships, this may help.
This is one approach. There may be more generalization that could be done,
perhaps combining all the mappings into one array. Certainly though, there
is no one-liner hiding for this problem.
So this is possible. There is getOptions() and getOptionsNoCase().
I found it interresting for my own use and, I thought I would drop
it back into here for anybody elses use. All mappings are now in one array,
and only one function call is needed per comparison of parameter pairs.
Since all the parts are broken out, there is many possibilities available
for error reporting. If the rigid standards are removed (its just interpretation),
the array of common parameter keys can be silently processed quite easily.
That is of interrest to me.
-sln
======================================
## options3.pl
##
use strict;
use warnings;
# indexes, if fully referenced option is needed
use constant OPT_LEGAL_ONLY => 0;
use constant OPT_USER_ONLY => 1;
use constant OPT_ALL => 2;
use constant OPT_COMMON => 3;
sub getOptions {
my ($legal, $user) = @_;
my @options = ([],[],[],[]);
# -- 'user only' and common
my %legalonly = %{ $legal };
for ( sort keys %{ $user } ) {
push @{ !exists $legal->{ $_ } ? $options[1] : $options[3] }, $_;
delete $legalonly{ $_ };
}
# -- legal only
@{ $options[0] } = sort keys %legalonly;
# -- all
@{ $options[2] } = sort keys %{ { map { $_ => 1 } (keys %{ $legal }, keys %{ $user }) } };
@options;
}
sub getOptionsNoCase {
my ($l, $u) = @_;
my $legal = { map { lc $_, 1 } keys %{ $l } };
my $user = { map { lc $_, 1 } keys %{ $u } };
getOptions( $legal, $user );
}
##
my $Debug = 1;
my %legalopts = (a => 1, b => 1, c => 1, d => 1);
my %useropts = (a => 1, c => 1, e => 1, f => 1, g => 1);
#my %useropts = (a => 1, b => 1, c => 1, d => 1); # try a sucessfull match
my ($legalonly, $useronly, $all, $common) = getOptions ( \%legalopts, \%useropts );
if ( $Debug ) {
print "\nDebug:\n";
print "All - ",@$all, ", elements = ", scalar(@$all), "\n";
print "Legal missing/non-matching - ",@$legalonly,", elements = ", scalar(@$legalonly), "\n";
print "User too many/non-matching - ",@$useronly, ", elements = ", scalar(@$useronly), "\n";
print "Common matching - ",@$common, ", elements = ", scalar(@$common), "\n";
}
if ( @$legalonly || @$useronly ) {
print "\nError:\n";
print "Legal options are '",( sort keys %legalopts ),"'\n";
print "Missing legal user options '",@$legalonly,"'\n" if (@$legalonly);
print "Non-legal user options '",@$useronly,"'\n" if (@$useronly);
} else {
print "\nSucessfully enterred legal options '",( sort keys %useropts ),"' !\n"
}
__END__
output:
-------
Debug:
All - abcdefg, elements = 7
Legal missing/non-matching - bd, elements = 2
User too many/non-matching - efg, elements = 3
Common matching - ac, elements = 2
Error:
Legal options are 'abcd'
Missing legal user options 'bd'
Non-legal user options 'efg'