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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Max Value

Status
Not open for further replies.

w33mhz

MIS
May 22, 2007
531
US
here is an easy question, is there a function in vbscript to find the largest value out of a set of numbers. I have been trying to use the Ubound function but I am not having luck here is my usage:
Code:
Function pickmailstore(strexgbox)
dim colItems
dim strComputer
dim objItem
dim i, j, k, l

strComputer = strexgbox
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & _
        "\ROOT\MicrosoftExchangeV2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Exchange_Mailbox")
arrTotal = Array(i,j,k,l)
i = 0
j = 0
k = 0
l = 0
On Error Resume Next
For Each objItem in colItems
	if objItem.StoreName = "Store1" then
	i = i + 1
	Elseif objItem.StoreName = "Store2" then
	j = j + 1
	Elseif objItem.StoreName = "Store3" then
	k = k + 1
	Elseif objItem.StoreName = "Store4" then
	l = l + 1
	End if
Next

return Ubound(arrTotal,1)

end Function

when I echo the function I don't get a value. I would think at least I would get "l"


 
UBound returns the size of the array, not the largest value in the array. Your UBound should be returning 3. Since it's not, you might want to turn off the On Error Resume Next.

If you want to know which i, j, k, or l is larger; you're probably going to have to just do comparisons. You could throw them in an array and sort the array. Then you take the largest value and do like a case statement.

With it only being 4 values, you could do it pretty easily with a comparison though..
Code:
Select Case TRUE
  Case i >= j And i >= k And i >= l
    MsgBox "i = " & CStr(i)
  Case j >= k And j >= l
    MsgBox "j = " & CStr(j)
  Case k >= j
    MsgBox "k = " & CStr(k)
  Case Else
    MsgBox "l = " & CStr(l)
End Select

I'm pretty sure that would work. If it's not case 1, then you know i isn't the biggest, so ignore it for the others. If it's not case 2, then you know i and j aren't the biggest.

This would give i preference over j, j over k, etc. in regards to them being the same value.
 
uh, if this is VBScript, you're

return Ubound(arrTotal,1) doesn't make sense.
should be

pickmailstore = Ubound(arrTotal,1)

Like SKIE suggested turning off on error resume next; at a min you should turn it off if you dont need the rest of the way. Also it will not do the MAX you're wanting like SKIE said too.
You have some major flaws in the logic if I read into your flow here.
First of all you're not returning the value of the function;
You set the array to = four elements that are equal to nothing?
Then initialze the elements to 0? which is not putting into array as such?
Then in the For Each loop, you are NOT acutally entering the data in the the array, just the variables?

Below is the revised code I'd suggest, but again will only spit out the ubound, i'd return the whole array or whever you want to figure out the max like using SKIE's example.

Your code should look like the following:

Function pickmailstore(strexgbox)
dim colItems
dim strComputer
dim objItem
dim i, j, k, l

strComputer = strexgbox
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & _
"\ROOT\MicrosoftExchangeV2")

Set colItems = objWMIService.ExecQuery _
("Select * from Exchange_Mailbox")

i = 0
j = 0
k = 0
l = 0
On Error Resume Next
For Each objItem in colItems
if objItem.StoreName = "Store1" then
i = i + 1
Elseif objItem.StoreName = "Store2" then
j = j + 1
Elseif objItem.StoreName = "Store3" then
k = k + 1
Elseif objItem.StoreName = "Store4" then
l = l + 1
End if
Next

arrTotal = Array(i,j,k,l)

pickmailstore = Ubound(arrTotal,1)

end Function

[yinyang] Tranpkp [pc2]
 
Skie,

Thank you, that seemed to point me in the right direction. Here is the code that I used:

Code:
Function pickmailstore(strexgbox)
dim colItems
dim strComputer
dim objItem
dim i, j, k, l

strComputer = strexgbox
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & _
        "\ROOT\MicrosoftExchangeV2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Exchange_Mailbox")

i = 0
j = 0
k = 0
l = 0
On Error Resume Next
For Each objItem in colItems
	if objItem.StoreName = "TMC1" then
	i = i + 1
	Elseif objItem.StoreName = "TMC2" then
	j = j + 1
	Elseif objItem.StoreName = "TMC3" then
	k = k + 1
	Elseif objItem.StoreName = "TMC4" then
	l = l + 1
	End if
Next

Select Case TRUE
  Case i >= j And i >= k And i >= l
    pickmailstore = CStr(i)
  Case j >= k And j >= l And j >=i
    pickmailstore =  CStr(j)
  Case k >= j And k >=l And j >= i
    pickmailstore = CStr(k)
  Case Else
    pickmailstore = CStr(l)
End Select
end Function

Tranpkp,
you know I am willing to admit that I have some error in my logic thus why I am posting a question.
You have some major flaws in the logic
You could have just stated that I defined my array to early in my code. But after trying it it seems your logic has some major flaws as well.
 
I'd replace this:
Case k >= j And k >=l And j >= i
with this:
Case k >= j And k >=l And [!]k[/!] >= i

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top