We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

# Array Help

on
Medium Priority
331 Views
Heres the deal... when Im posting to my logic page, Im posting in a list of order numbers. I read in this array of orders then I need to display them i.e.

1-5, 6,8,9-20

Its working ok except that it seems to leave off the last order number. For instance heres the output...

Batch ID: 4152 Order(s): 42163-42165,42171,42176- on 3/22/2004

when I actually had 42176,42177,42178 in the array. It should have said....

Batch ID: 4152 Order(s): 42163-42165,42171,4217642188- on 3/22/2004

Any idea where this code is going wrong?
Heres sample values in the arrid array:

arrid(0)=42163
arrid(1)=42164
arrid(2)=42165
arrid(3)=42171
arrid(4)=42176
arrid(5)=42177
arrid(6)=42178
arrid(7)=42179
arrid(8)=42180
arrid(9)=42181
arrid(10)=42182

Thanks!
-Matt

orders=Split(request.form, "&", -1, 1)

numorders=ubound(orders)+1
Dim arrid
redim arrid(numorders)
temparray=Split(request.form, "&", -1, 1)

'Reverse the array
for iTmp1=lBound(temparray) to ubound(temparray)
for iTmp2=iTmp1+1 to ubound(temparray)
if temparray(iTmp1) > temparray(iTmp2) then
tmp = temparray(iTmp2)
temparray(iTmp2) = temparray(iTmp1)
temparray(iTmp1) = tmp
end if
next
next
for x=0 to ubound(temparray)
arrid(x)=cdbl(Replace(temparray(x), "=ON",""))
'print "arrid("&cstr(x)&")="&cstr(arrid(x))
next

strOutput = ""
lowIndex = lbound(arrID)     'keep track of the index
upperIndex = ubound(arrID)

for i = lowIndex+1 to upperIndex
if arrID(i) > arrID(i-1) + 1 then     'if the difference is greater than 1
if (i-1) = lowIndex then     'example: arrID(2) to arrID(3)
strOutput = strOutput & "," & arrID(i-1)
elseif (i-1) = lowIndex + 1 then     'example: arrID(0) and arrID(1) when i=2
strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(i-1)
else          'example: arrID(3) to arrID(5) when i=6
strOutput = strOutput & "," & arrID(lowIndex) & "-" & arrID(i-1)
end if
lowIndex = i
end if
next
'handle the last index which was not handle before breaking out of the loop
if upperIndex > lowIndex + 1 then
strOutput = strOutput & "," & arrID(lowIndex) & "-" & arrID(upperIndex)
elseif upperIndex = lowIndex + 1 then
strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(upperIndex)
else
strOutput = strOutput & "," & arrID(upperIndex)
end if

Response.Write(Right(strOutput,len(strOutput)-1))    'Right() to remove the first ","
Comment
Watch Question

## View Solution Only

Commented:
hi Matt,

strange, the output using the arrid() sample values you provided does give the right result:

<%
dim arrid(10)

arrid(0)=42163
arrid(1)=42164
arrid(2)=42165
arrid(3)=42171
arrid(4)=42176
arrid(5)=42177
arrid(6)=42178
arrid(7)=42179
arrid(8)=42180
arrid(9)=42181
arrid(10)=42182

strOutput = ""
lowIndex = lbound(arrID)     'keep track of the index
upperIndex = ubound(arrID)

for i = lowIndex+1 to upperIndex
if arrID(i) > arrID(i-1) + 1 then     'if the difference is greater than 1
if (i-1) = lowIndex then     'example: arrID(2) to arrID(3)
strOutput = strOutput & "," & arrID(i-1)
elseif (i-1) = lowIndex + 1 then     'example: arrID(0) and arrID(1) when i=2
strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(i-1)
else          'example: arrID(3) to arrID(5) when i=6
strOutput = strOutput & "," & arrID(lowIndex) & "-" & arrID(i-1)
end if
lowIndex = i
end if
next
'handle the last index which was not handle before breaking out of the loop
if upperIndex > lowIndex + 1 then
strOutput = strOutput & "," & arrID(lowIndex) & "-" & arrID(upperIndex)
elseif upperIndex = lowIndex + 1 then
strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(upperIndex)
else
strOutput = strOutput & "," & arrID(upperIndex)
end if

Response.Write(Right(strOutput,len(strOutput)-1))    'Right() to remove the first ","
%>

the output on my screen is:   42163-42165,42171,42176-42182
which is correct, right?

maybe write out the output for the array in your code and check to make sure arrid() is generated correctly.  check before and after the strOutput codes if arrid(upperIndex) contains the value '42182' or not...

Commented:
Kingsfan,

ok... checked the arrid. It looks like its being created ok... The problem seems to be more evident depending on the order or data that I have in the arrid. For example:

arrid(0)=42175
arrid(1)=42176
arrid(2)=42178
arrid(3)=42180
arrid(4)=42182

gives me

Batch ID: 4182 Order(s):
42175,42176,42178,42180,42182,42182 on 3/22/2004

which should be:
42175-42176,42178,42180,42182 on 3/22/2004

Any idea why the logic of the code isnt doing what its supposed to? Thanks again!
-Matt

Commented:
Matt,

there are two conditions you want to make sure before applying the array to the strOutput codes.

1.  make sure the numbers are in ascending order.  if they are not always in ascending order, you'll need to sort the array first.

2.  make sure the dimension of arrid() is the same as the array temparray()

you can redim the arrid() size like this:

dim arrid()

arrSize = UBound(temparray) - LBound(temparray)
redim arrid(arrSize)

hope it helps

Commented:
Kingsfan,

Thanks for your help however I think theres still a bug in this somewhere. When Im posting to this page, initially all of the values are highest to lowest but they are in order as they are coming from SQL. So Im reverse sorting the array in the code which seems to work ok. Im not sure where to put that code about redimming the array you spoke of though. Heres what I currently have...

Im wondering because arrid is zero based, wonder if the logic code works ok starting at element zero?

the sample values...

arrid(0)=42179
arrid(1)=42180
arrid(2)=42183
arrid(3)=42185
arrid(4)=42186
arrid(5)=42187
arrid(6)=42188
arrid(7)=42189

will print out 42179,42180,42183,42185-42189 which is ALMOST correct but it should be:
42179-42180,42183,42185-42189

-Matt

orders=Split(request.form, "&", -1, 1)
numorders=ubound(orders)+1
Dim arrid
temparray=Split(request.form, "&", -1, 1)
arrSize = UBound(temparray)
redim arrid(arrSize)

'Reverse the array
for iTmp1=lBound(temparray) to ubound(temparray)
for iTmp2=iTmp1+1 to ubound(temparray)
if temparray(iTmp1) > temparray(iTmp2) then
tmp = temparray(iTmp2)
temparray(iTmp2) = temparray(iTmp1)
temparray(iTmp1) = tmp
end if
next
next
for x=0 to ubound(temparray)
arrid(x)=cdbl(Replace(temparray(x), "=ON",""))
print "arrid("&cstr(x)&")="&cstr(arrid(x))
next

strOutput = ""
lowIndex = lbound(arrID)     'keep track of the index
upperIndex = ubound(arrID)

print "lowindex= "&cstr(lowindex)
print "upperIndex= "&cstr(upperIndex)

for i = lowIndex to upperIndex
print "I="&cstr(I)
if arrID(i) > arrID(i-1) + 1 then     'if the difference is greater than 1
if (i-1) = lowIndex then     'example: arrID(2) to arrID(3)
strOutput = strOutput & "," & arrID(i-1)
elseif (i-1) = lowIndex + 1 then     'example: arrID(0) and arrID(1) when i=2
strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(i-1)
else          'example: arrID(3) to arrID(5) when i=6
strOutput = strOutput & "," & arrID(lowIndex) & "-" & arrID(i-1)
end if
lowIndex = i
end if
next
'handle the last index which was not handle before breaking out of the loop
if upperIndex > lowIndex + 1 then
strOutput = strOutput & "," & arrID(lowIndex) & "-" & arrID(upperIndex)
elseif upperIndex = lowIndex + 1 then
strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(upperIndex)
else
strOutput = strOutput & "," & arrID(upperIndex)
end if

Response.Write(Right(strOutput,len(strOutput)-1))    'Right() to remove the first ","

Commented:
Matt,

i think your redim code looks fine.

for the sample array you listed above that is "ALMOST" correct, actually i think it is correct according to the specification you gave on your last post.  if you look back at the question your posted last time on this problem, the sample was like this:

1=ON
2=ON
5=ON
100=ON
101=ON
102=ON
105=ON
106=ON
107=ON
108=ON
109=ON

and output you want was:  1,2,5,100-102,105-109

so i assume for 2 consecutive number (1,2) you want to seperate them by "," and if there are more than 2 consecutive number then seperate them by "-".
but if you actually want to show as 1-2 instead of 1,2 as the example above, i should be able to modify the code to do that.  just need your confirmation if that's the case

kingsfan
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)

Commented:
Kingsfan,

Youre a genius!!! Thank you! Works awesome now-Matt

Commented:
glad you like it and thanks for the compliment!

if you don't mind you can close the other post on this question, either accept the answer or request to delete it if you think the question is answered in this post instead of the other one.  thanks.

kingsfan
Unlock the solution to this question.