
Need help with algorithm
I have this array that can hold any numbers of records(for example: 29 or 129 or 150 or 2347 or 5896 or ...)<BR><BR>Now I need to send an email each time the loop of my array is a factor of 50.<BR><BR>The thing is if the array contains less records then 50 I need to send an email according to that number.<BR><BR>On the other hand if the array is larger then 50(for example 129) I need to send 3 times 50 emails and one time 29 emails<BR><BR>What would be my algorithm<BR><BR>Sincerely<BR>Vlince

Is this a test?
Simple math:<BR>numofelts = UBound(yourarray)<BR>If numofelts < 50 Then<BR> numofemails = 1<BR>Else<BR> numofemails = numofelts/50<BR> remainder = numofelts Mod 50<BR> If remainder <> 0 Then<BR> numofemails = numofemails + 1<BR> End If<BR>End If<BR><BR>I think that'll work for you.

1...2...3...1...2...3...1...2...3...
That's a nice waltz allgo rhythm.<BR><BR>Oh, okay...<BR><BR>MOD. Look up the MOD operator in the VBScript reference.<BR><BR>And I sure *HOPE* you don't mean what you wrote:<BR> if the array is larger then 50(for example 129) <BR> I need to send 3 times 50 emails and one time 29 emails<BR><BR>Ummm...50 + 50 + 50 + 29 is 179 on my fingers and toes (I have lots of toes).<BR><BR>Anyway:<BR><BR>I presume the point of this is to send email to no more than 50 addresses at a time?<BR><BR><%<BR>addresses = ... an array of unknown size ...<BR><BR>For i = 0 To UBound( addresses )<BR> If (i MOD 50) = 0 Then ' every 50 addresses<BR> If i <> 0 Then ' (don't send if no addresses yet)<BR> objMail.to = Mid( ccs, 2 )<BR> objMail.Send ' send last mail<BR> End If<BR> Set objMail = ... ' create another email object<BR> ' (Note: May be able to reuse same one; depends on object)<BR> ccs = "" ' start with no addresses<BR> End If<BR> ccs = ccs & ";" & addresses(i) ' tack on an address<BR>Next<BR>' clean up last group, if any...<BR>If ccs <> "" Then<BR> objMail.to = Mid( ccs, 2 )<BR> objMail.Send ' send last mail<BR>End If<BR>%><BR><BR>The Mid( ccs, 2 ) is so that you chop off the semicolon before the first name.<BR><BR><BR><BR><BR><BR>

Extra code in that...and bug
numofelts = UBound(yourarray)<BR>numofemails = numofelts[hl="yellow"] [/hl]50 ' needs INTEGER division<BR>remainder = numofelts Mod 50<BR>If remainder <> 0 Then<BR> numofemails = numofemails + 1<BR>End If<BR><BR>YOu did ordinary division, so if there were 129 addresses, your numofelts would end up being 3.58 ... say oops.<BR><BR>The backslash operator in VBScript does integer division and tosses the remainder away.<BR><BR>And why bother for a test for less than 50? The code works fine, without that test.<BR><BR>

that popping sound is my foot coming out of my mou
You're absolutely right. I keep trying, and you keep teaching me how to do it better. Maybe I should stick to asking questions...

Thank you thank you
I knew I could count on you Bill<BR><BR>I owe you one :)<BR><BR>Sincerely Thanks<BR>Vlince

Nonsense! Yours was better!
In practice, he/you would have found the numberofemails bug fast and simply done INT(numberofemails) in the FOR loop to fix it.<BR><BR>I think, actually, I like your approach more than my own. Cleaner, if we write the rest of the code:<BR><BR><%<BR>addresses = ... the array ....<BR><BR>numAddresses = UBound( addresses ) + 1<BR>numEmails = numAddresses 50<BR>If numAddresses MOD 50 <> 0 Then numEmails = numEmails + 1<BR>For email = 1 To numEmails<BR> Set objMail = ...<BR> startat = 50 * ( email  1 )<BR> stopat = startat + 49<BR> If stopat >= numAddresses Then stopat = numAddresses1<BR> ccs = ""<BR> For addr = startat To stopat<BR> ccs = ccs & ";" & addresses(addr)<BR> Next<BR> objMail.to = Mid( ccs, 2 )<BR> objMail.Send<BR>Next<BR>%><BR><BR>Really, that's easier to understand and cleaner!<BR><BR>I should have thought of your approach. Don't sweat the small stuff, like minor bugs. You'll always find those when you start testing your code. Concentrate on the big stuff, like overall logic! You've got good instincts, which is the important part.<BR><BR><BR><BR>

Yeah, but look at Neophyte's...
...start on the code and how clean the final solution from that is. Simpler than this subthread, by far.<BR><BR>

nice code, slick stuff
thanks for the vote of confidence

Sorry neophyte
I did forget to thank you for your input<BR><BR>It is not like me to forget :(<BR><BR>I'll remember!<BR><BR>Many thanks neophyte <BR>Sincerely<BR><BR>Vlince
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

