Solved

Array Help

Posted on 2004-03-22
8
291 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now