Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Array Help

Posted on 2004-03-22
8
Medium Priority
?
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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