Perhaps this has been covered elsewhere, but can't find a reference to
it in FAQ and through diligent searching.
In some languages, it's possible to perform a runtime eval() that
essentially constructs a constant or other components of a statement and
executes it, returning the result. This is very handy in cases where
putting repetitive operations in a nice clean loop is an advantage.
Here's an example of a problem I'm working on...
The user inputs a range of ports that will be forwarded in the U/I of an
iptables management system I'm building. iptables doesn't accomodate
port range in forwarding, therefore you have to build a new statement
for each port.
The architecture, for reasons of sanity, allows 6 total forwarding input
lines and each holds a label, the to port, the from port (range), and
the target ip to forward to. Each variable is held in an array and
referenced by a pre-established constant.
Rather than write out each statement discretely, I'd rather put
processing for variables 1-6 in a loop and therefore need to reference
each constant within the loop:
for (i=0;i<7;i++)
{
for (j=nStartPort;j<=nEndPort;j++)
{
aVar[PORT_START_(j)] = GetStartPort(j);
aVar[PORT_END_(j)] = GetEndPort(j);
}
}
GetStartPort and GetEndPort return an integer based on the users entry
of port range for the entry line (i) that we are currently working.
I'd rather do this:
for (i=0;i<7;i++)
{
for (j=nStartPort;j<=nEndPort;j++)
{
sprintf(cString,"aVar[PORT_START_%i]");
eval(cString) = GetStartPort(j);
...
}
}
Or something equivalent, where eval(..) would (I guess) act as a pointer
to the variable being referenced.
Now that I write, I realize I could (perhaps) reference a pointer to
each variable in an array and point to that, but it *may* take some of
the modularity and flexibility I hoped to achieve away.
Well, am I barking up the wrong tree here? Maybe there's another way to
do this?