http://www.perlmonks.org?node_id=613249


in reply to Storing multiple arguments in a data structure that allows for future expansion

Trying to understand exactly what you mean by "more arguments".

Are you saying that you currently have arguments -i, -p, and -o, and your customer wants to add arguments -x, -y, and -z?

Or, are you saying you currently handle two arguments for each parameter, and your customer wants to use 5 for each parameter?

If you're talking about the former, I'd imagine you'd have to make some mods in your program to do that anyway. Before the processOne subroutine only is taking three paramters. Now, you'd have to not only pass in five parameters, but modify the code to actually use them. If you are referring to the same three parameters, but more values for them. You could do something like this:

my (@opt_i_list, @opt_o_list, @opt_p_list); # # ####Put options into Arrays # if ($opt_i) { @opt_i_list = split(",", $opt_i); } else { #No $opt_i parameter: Use default $opt_i_list[0] = $DEFAULT_OPT_I_VALUE; } if ($opt_o) { @opt_o_list = split(",", $opt_o); } else { $opt_o_list[0] = $DEFAULT_OPT_O_VALUE; } if ($opt_p) { @opt_p_list = split(",", $opt_p); } else { $opt_p_list[0] = $DEFAULT_OPT_P_VALUE; } # # ####Now run processOne for each arrangement of parameters # foreach my $opt_i_value (@opt_i_list) { foreach my $opt_o_value (@opt_o_list) { foreach my $opt_p_value (@opt_p_list { processOne('$opt_i_value',$opt_p_value,$opt_o_value) } } }
That will process all values of all the options against each other. So, if you have two options for each, the processOne subroutine will execute eight times. If there are 3 of each parameter, the processOne subroutine will execute 27 times.

If you add a parameter -x, you'd have to modify your processOne subroutine anyway. Adding another foreach loop in that structure would probably be relatively simple compared to the other coding changes you'd have to make. Is it efficient? Well, the number of times a loop has to process quickly grows with each additional value. That's not exactly efficient, but that's what you said you wanted.

  • Comment on Re: Storing multiple arguments in a data structure that allows for future expansion
  • Download Code