Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamically incrementing an object name inside a Loop in Perl

Posted on 2007-10-03
12
Medium Priority
?
227 Views
Last Modified: 2010-03-05
Okay, I have an array of users, and I'm trying to send an email to them....  How do I change the object
name dynamically?

      foreach $user_name (@users) {
      my $recipient = $msg->Recipients->Add();
      $recipient->{Name} = $user_name;
      $recipient->Resolve();
      }

each time through the foreach loop $recipient needs to change so the first time through it should read:
      my $recipient = $msg->Recipients->Add();
      $recipient->{Name} = "User One";
      $recipient->Resolve();
and second time:
      my $recipient2 = $msg->Recipients->Add();
      $recipient2->{Name} = "User One";
      $recipient2->Resolve();
and so on..
Can I change the object name dynamically each time through the loop like this:
my $recipient1
my $recipient2
my $recipient3?

Thanks!

0
Comment
Question by:mschwade
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 20009473
Without seeing all your code, why don't you just do the usual

loop through recipients
  send mail to recipient

That way you don't need to use an array to store the email.
0
 
LVL 1

Author Comment

by:mschwade
ID: 20009540
Im not storing email in the array.... The array has each user in there...

when I get to the point of adding the recipients,  I begin a loop through the user array to add each user to the email

but in order to do that, each Add() needs to be a separate object, i.e. if I statically added two recipients:

my $recipient = $msg->Recipients->Add();
$recipient->{Name} = "User One";
$recipient->Resolve();
my $recipient2 = $msg->Recipients->Add();
$recipient2->{Name} = "User Two";
$recipient2->Resolve();

Then I proceed to the rest of the email before sending and signing off the session

Thanks!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20009586
What mail module are you using?

Do you want to single a single email to multiple receipients or multiple emails to single recipients?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:mschwade
ID: 20009606
using MAPI

   use Win32::OLE;
   my $session = Win32::OLE->new("MAPI.Session")

Want to send single email to multiple recepients

Thanks!
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 2000 total points
ID: 20009996
Try this:
foreach (@users) {
    my $recip = $msg->Recipients->Add();
    $recip->{Name} = $_;
    $recip->Resolve();
}

Or this:
my @recip;
foreach (@users) {
    push @recip, $msg->Recipients->Add();
    $recip[-1]->{Name} = $_;
    $recip[-1]->Resolve();
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 20010313
> Can I change the object name dynamically each time through the loop like this:
> my $recipient1
> my $recipient2
> my $recipient3?
What do you wish to accomplish by this?
Each each time through the loop, the my instantiates a new variable, so the name already is dynamic.
0
 
LVL 1

Author Comment

by:mschwade
ID: 20013501
Your first suggestion didn't work Adam (The below code didn't work Adam)... I am not sure I understand your second code...  I notice it is pushing to an array does that mean I need to go and do a foreach on the @recip array?

Adam314 wrote:

foreach (@users) {
    my $recip = $msg->Recipients->Add();
    $recip->{Name} = $_;
    $recip->Resolve();
}


0
 
LVL 1

Author Comment

by:mschwade
ID: 20013583
I tried it as well, it didn't work.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20014423
What happened with the second code?
0
 
LVL 1

Author Comment

by:mschwade
ID: 20014488
Nothing.. neither of them error'd out, but neither of them relayed an email.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20014590
Just to be sure, try this:

my $recip1=$msg->Recipients->Add();
$recip1->{Name} = $users[0];
$recip1->Resolve();

my $recip2=$msg->Recipients->Add();
$recip2->{Name}=$users[1];
$recip2->Resolve();

Does that work for 2 names?
0
 
LVL 1

Author Comment

by:mschwade
ID: 20016625
I figured it out, it was bombing out without an error because there were two null entries to begin the array and it wouldn't go on from there.

Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question