×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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/www/html/1.html";
   my $1filename = "1_instructions.html";
   my $path_to_3 = "/var/www/html/3_instructions.html";
   my $3filename = "3_instructions.html";
   my $path_to_4 = "/var/www/html/4_instructions.html";
   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;
} 

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

You should write a sub that can send any combinations of attachments: the sub should receive a list of the attachments, such as (1,3,4) (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/www/html/$_.html",
      Filename => $_.'_instructions.html,
      Disposition => "attachment");
  }
  $msg->send();
  &logit;
} 

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

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

(OP)
prex1

Many thanks!



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close