Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Finding midpoint of an array with odd number of records

Posted on 2006-07-17
Medium Priority
305 Views
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
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
• 6
• 6
• 4

LVL 9

Expert Comment

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

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

ID: 17123872
How do you do that?
0

LVL 9

Expert Comment

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

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

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

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

LVL 9

Accepted Solution

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

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

ID: 17125340
Dana and I think alike it seems.
0

LVL 9

Expert Comment

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

ID: 17125478
CyrexCore

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

LVL 14

Expert Comment

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

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

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

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

Question has a verified solution.

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

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â€¦
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â€¦
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: â€¦
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompaâ€¦
###### Suggested Courses
Course of the Month4 days, 19 hours left to enroll