Solved

Array Help

Posted on 2004-03-22
8
297 Views
Last Modified: 2008-03-03
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 ","
0
Comment
Question by:mjreine
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:kingsfan76
ID: 10652968
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...
0
 

Author Comment

by:mjreine
ID: 10653208
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
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 10653893
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
0
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.

 

Author Comment

by:mjreine
ID: 10658986
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

I appreciate your help. Thanks!
-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 ","
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 10660123
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
0
 
LVL 12

Accepted Solution

by:
kingsfan76 earned 500 total points
ID: 10660331
matt,

for the sample you listed, if you want the output to look like
42179-42180,42183,42185-42189
all you need to do is comment out the elseif part and a minor change:

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     <<--- comment out
       '    strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(i-1)   <<-- comment out
       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 then    '<<--- change
    strOutput = strOutput & "," & arrID(lowIndex) & "-" & arrID(upperIndex)
'elseif upperIndex = lowIndex + 1 then      <<-- comment out
'     strOutput = strOutput & "," & arrID(lowIndex) & "," & arrID(upperIndex)  <-- com't out
else
    strOutput = strOutput & "," & arrID(upperIndex)
end if
0
 

Author Comment

by:mjreine
ID: 10661057
Kingsfan,

 Youre a genius!!! Thank you! Works awesome now-Matt
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 10661174
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
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update static table with two columns and multiple rows... 15 89
SQL Filter Question 8 85
age from date of birth 4 51
Connecting to multiple databases to create a Dashboard 5 47
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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