Solved

Finding midpoint of an array with odd number of records

Posted on 2006-07-17
16
297 Views
Last Modified: 2008-03-04
  I am doing a search on a database. It is pulling up all records that match the search. I want it to divide the records in two and put the first half in the left hand column, then the 2nd half in the right hand column of a table. It works fine if I have an even number of records that match, but if there is an odd number, it does the left column, then duplicates the values in the 2nd column and repeats them.

Any suggestions?

 <%
dim aRS, x,y
aRS = RS.GetRows()
RS.close
x = ubound(aRS,2) / 2
y = 0
do while y < x
 if x + y < ubound(aRS,2) then
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x+y) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x+y) & "</font></td></tr>"
  else
     response.write "<tr bgcolor=""white""><td>" & aRS(1,y) & "</td><td>&nbsp;</td></tr>"
  end if

   y = y + 1
loop
%>
0
Comment
Question by:katlees
[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
  • 6
  • 6
  • 4
16 Comments
 
LVL 9

Expert Comment

by:danataylor
ID: 17123764
See if this works instead of x = ubound(aRS,2) / 2

if ubound(aRS,2) / 2 = cint(ubound(aRS,2) / 2) then
    x = ubound(aRS,2) / 2
else
    x = cint(ubound(aRS,2) / 2) + 1
end if
0
 
LVL 9

Expert Comment

by:danataylor
ID: 17123783
You will also need to do a bounds check for x+y in the aRS(1,x+y) part because it will result in an "out of bounds" error when there are an odd number of elements.
0
 

Author Comment

by:katlees
ID: 17123872
How do you do that?
0
Technology Partners: 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!

 
LVL 9

Expert Comment

by:danataylor
ID: 17124061
Actually, I believe this should take care of it.

if ubound(aRS,2) / 2 = cint(ubound(aRS,2) / 2) then
    x = ubound(aRS,2) / 2
else
    x = cint(ubound(aRS,2) / 2) + 2
end if

y = 0
for y=lbound(aRS,2) to cint(ubound(aRS,2) / 2)
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x+y) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x+y) & "</font></td></tr>"
next

if x+y < cint(ubound(aRS,2)) then
    response.write "<tr bgcolor=""white""><td>" & aRS(1,y) & "</td><td>&nbsp;</td></tr>"
end if


0
 

Author Comment

by:katlees
ID: 17124343
I get this..
Microsoft VBScript runtime error '800a0009'
Subscript out of range: '[number: 80]'

/meds/allianzunmedgood.asp, line 109


109 is the response.write line

Here is the code how I have it on the page - was the loop part supposed to be taken out?

    <%
dim aRS, x,y
aRS = RS.GetRows()
RS.close
if ubound(aRS,2) / 2 = cint(ubound(aRS,2) / 2) then
    x = ubound(aRS,2) / 2
else
    x = cint(ubound(aRS,2) / 2) + 2
end if

y = 0
for y=lbound(aRS,2) to cint(ubound(aRS,2) / 2)
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x+y) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x+y) & "</font></td></tr>"
next

if x+y < cint(ubound(aRS,2)) then
    response.write "<tr bgcolor=""white""><td>" & aRS(1,y) & "</td><td>&nbsp;</td></tr>"
end if

 
%>
0
 
LVL 9

Expert Comment

by:danataylor
ID: 17124414
Yeah, I know.  Let me work on it a little bit.

After digging into your code a little deeper I have some questions

1. How many columns (fields) are returned in teh recordset?
2. It looks like the array is two-dimensional with 2 columns (0 & 1) and an unknown number of rows.
2. You are only referencing data in column 1. (aRS(1,y), aRS(1,x+y), etc)
3. Using x and y to reference the same array element (aRS(1,y), aRS(1,x+y), etc) is a little confusing.  x is typically used to reference the column while y references the row - just like cartesian coordinates.  I would suggest using something like j & k or j & o.  o would indicate an offset like the value that x represents in your code.

I'll get back to you in a minute.
0
 

Author Comment

by:katlees
ID: 17124555
The number of rows varies upon the search. Sometimes it is 5, sometimes it is 40
0
 
LVL 9

Accepted Solution

by:
danataylor earned 500 total points
ID: 17125299
I usually do stuff like this using nested tables where one crates a table with two columns and then defines an inner table for each of the columns.  I recoded your stuff this way.  I hope it's right.

if ubound(aRS,2) / 2 = int(ubound(aRS,2) / 2) then
  x = int(ubound(aRS,2) / 2) -1
else
  x = int(ubound(aRS,2) / 2)
end if

Response.Write "<table>" & vbcrlf                           'outer table start
response.write "<tr>" & vbcrlf
response.write "<td>" & vbcrlf                                 ' outer table left column

Response.Write "<table>" & vbcrlf                           ' left inner table start
for y=lbound(aRS,2) to x
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td></tr>" & vbcrlf
next
Response.Write "</table>" & vbcrlf                            ' left inner table end
response.write "</td>" & vbcrlf

if ubound(aRS,2) / 2 = int(ubound(aRS,2) / 2) then       ' if odd number skip one to leave the middle
      y=y+1                                                       ' record for the end of the left column
end if

response.write "<td>" & vbcrlf                                     ' outer table right column
Response.Write "<table>" & vbcrlf                               ' start of right inner table
for y=y to ubound(aRS,2)
    response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,y) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,y) & "</font></td></tr>" & vbcrlf
next
Response.Write "</table>" & vbcrlf                              'end of right inner table

response.write "</td>" & vbcrlf
response.write "</tr>" & vbcrlf
if ubound(aRS,2) / 2 = int(ubound(aRS,2) / 2) then              ' check to see if odd and then output the middle record
    response.write "<tr bgcolor=""white""><td>" & aRS(1,int(ubound(aRS,2) / 2)) & "</td><td>&nbsp;</td></tr>" & vbcrlf
end if
Response.Write "</table>" & vbcrlf                                    'end of outer table
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125335
Why not just do it this way?

<table><tr><td valign=top><table>
<%
dim aRS, x, half_rows, total
aRS = RS.GetRows()
RS.close
half_rows = (ubound(aRS,2) + 1) / 2 + (ubound(aRS, 2) mod 2)
for x = 0 to (half_rows - 1)
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
Response.Write "</table></td><td valign=top><table>"
total = ubound(aRS, 2) + 1 '+1 for count as opposed to the last index
for x = half_rows to total - 1
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & ":</font></b>&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
%>
</table></td></tr></table>

That should work just fine? If there's an odd number of elements the extra will show on the left side.
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125340
Dana and I think alike it seems.
0
 
LVL 9

Expert Comment

by:danataylor
ID: 17125409
I didn't think of using "mod 2" to get the remainder and adding it to the 1st half of the array count.  Less code is always better.
0
 

Author Comment

by:katlees
ID: 17125478
CyrexCore

Yours gives me 4 columns, 1st column twice, then 2nd column twice
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125685
Oh oh my mistake I didn't edit your code enough.

<table><tr><td valign=top><table>
<%
dim aRS, x, half_rows, total
aRS = RS.GetRows()
RS.close
half_rows = (ubound(aRS,2) + 1) / 2 + (ubound(aRS, 2) mod 2)
for x = 0 to (half_rows - 1)
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
Response.Write "</table></td><td valign=top><table>"
total = ubound(aRS, 2) + 1 '+1 for count as opposed to the last index
for x = half_rows to total - 1
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
%>
</table></td></tr></table>
0
 

Author Comment

by:katlees
ID: 17125837
CyrexCore,

Your code did two columns, but the first column has two more records than the 2nd on oens that have an even count.

Dana's works great so I will just use that.

Thanks guys
0
 
LVL 14

Expert Comment

by:CyrexCore2k
ID: 17125855
If you're still interested this should work.

<table><tr><td valign=top><table>
<%
dim aRS, x, half_rows, total
aRS = RS.GetRows()
RS.close
half_rows = (ubound(aRS,2) + 1) / 2 + ((ubound(aRS, 2) + 1) mod 2)
for x = 0 to (half_rows - 1)
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
Response.Write "</table></td><td valign=top><table>"
total = ubound(aRS, 2) + 1 '+1 for count as opposed to the last index
for x = half_rows to total - 1
     response.write "<tr><td><b><font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""000000"">" & aRS(1,x) & "</font></b>:&nbsp;<font face=""Arial, Helvetica, sans-serif"" size=""2"" color=""66024b"">" & aRS(2,x) & "</font></td></tr>"
next
%>
</table></td></tr></table>
0
 

Author Comment

by:katlees
ID: 17126346
CyrexCore,

Thank you. Problem with two tables - one for each column, is if one colum goes to two lines, the rows don't match up and it looks funny.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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