Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI perl that will list and include all non-repeating combinations

Status
Not open for further replies.

raggmopp

MIS
Feb 14, 2002
40
0
0
US
Hi all:

I have a perl CGI script that has checkboxes for 4 different elements. Call the elements 1 2 3 4.
Depending on the selections made an email attachment is included.

Example, check 1 & 3 and the email will have the attachments corresponding to 1 & 3
So with 4 elements there are a possible 16 non-repeatable combinations - NOT permutations.

Code:
1, 2, 3, 4, 1&2, 1&3, 1&4, 2&3, 2&4, 1&2&3, 1&2&4, 2&3&4, etc...

There are 2**4 possibilities, including NULL.

Since there are 4 elements = 16 possible combinations, I just hard coded the 16 possibilities thinking I will find a solution later.
Later has come. I need to add a 5th element meaning there are now 32 possible non-repeating combinations. 2**5 = 32
And if I have to add a 6th element, which is a very real possibility, there would be 64 possible combinations.

Example of what I am doing now. And my question is, how can I do it better?
Code:
sub mailit134 {
   my $path_to_1 = "/var/[URL unfurl="true"]www/html/1.html";[/URL]
   my $1filename = "1_instructions.html";
   my $path_to_3 = "/var/[URL unfurl="true"]www/html/3_instructions.html";[/URL]
   my $3filename = "3_instructions.html";
   my $path_to_4 = "/var/[URL unfurl="true"]www/html/4_instructions.html";[/URL]
   my $4filename = "4_instructions.html";
   my $msg = MIME::Lite->new(
      Subject  =>  "Support",
      From     =>  "Support",
      To       =>  $cust,
      Type     =>  'text/html',
      Encoding =>  '7bit',
      Data     =>  $comments);
   $msg->attach(
      Type => 'text/html',
      Path => $path_to_1,
      Filename => $1filename,
      Disposition => "attachment");
   $msg->attach(
      Type => 'text/html',
      Path => $path_to_3,
      Filename => $3filename,
      Disposition => "attachment");
   $msg->attach(
      Type => 'text/html',
      Path => $path_to_4,
      Filename => $4filename,
      Disposition => "attachment");
   $msg->send();
   &logit;
}
 
You should write a [tt]sub[/tt] that can send any combinations of attachments: the sub should receive a list of the attachments, such as [tt](1,3,4)[/tt] (undefs or null values in the list will be skipped).
Code:
sub mailit {
  local($_);
  my $msg = MIME::Lite->new(
    Subject  =>  "Support",
    From     =>  "Support",
    To       =>  $cust,
    Type     =>  'text/html',
    Encoding =>  '7bit',
    Data     =>  $comments);
  for(@_){
   next unless $_;
   $msg->attach(
      Type => 'text/html',
      Path => "/var/[URL unfurl="true"]www/html/$_.html",[/URL]
      Filename => $_.'_instructions.html,
      Disposition => "attachment");
  }
  $msg->send();
  &logit;
}

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top