I have two types of objects: M
and N
, identifiable by a name. Additionally, multiple M
s can be grouped into one or more M-Group
(also identifiable by a name), same for N
s. (If it makes your life easier, you can assume that nesting is not allowed, however nesting would be nice.)
A Rule
consists of multiple M
s and multiple N
s. If I have a list of Ms = [M(1), M(2), M(3)]
and a list of Ns = [N(A), N(B), N(C)]
, I need to generate a list of rules that looks like this:
M=1, N=A
M=2, N=A
M=3, N=A
M=1, N=B
M=2, N=B
M=3, N=B
M=1, N=C
M=2, N=C
M=3, N=C
As multiple M
s can be grouped into M-groups, assuming that our Ms
list is condensed into a M-Group
called MG1
, then my list of text would look like this:
MG=MG1, N=A
MG=MG1, N=B
MG=MG1, N=C
If we group Ns
in a similar way (NG1
), then my list of rules would look like this:
MG=MG1, NG=NG1
Obviously this can be done pretty easily with two layers of for
loops, but the added complexity of the user choosing to output the rules with groups instead of individual M
s and N
s means that I would need to check whether the user is using a group, therefore I would need to write 4 if
blocks (one for M-Group
-> N
, one for M
-> N-Group
, one for both, and one for neither). If we add another dimension O
, then I would need 16 if
blocks to cater for all solutions, etc.
Is there is a solution that will allow me to generate this list without writing 2^n
if
blocks (where n
is the dimension in the array)?
(To those wondering: Yes, this is in the context of generating firewall access rules)
2