Link to home
Start Free TrialLog in
Avatar of katlees
katleesFlag for United States of America

asked on

Finding midpoint of an array with odd number of records

  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
%>
Avatar of danataylor
danataylor

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
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.
Avatar of katlees

ASKER

How do you do that?
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


Avatar of katlees

ASKER

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

 
%>
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.
Avatar of katlees

ASKER

The number of rows varies upon the search. Sometimes it is 5, sometimes it is 40
ASKER CERTIFIED SOLUTION
Avatar of danataylor
danataylor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CyrexCore2k
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.
Dana and I think alike it seems.
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.
Avatar of katlees

ASKER

CyrexCore

Yours gives me 4 columns, 1st column twice, then 2nd column twice
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>
Avatar of katlees

ASKER

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
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>
Avatar of katlees

ASKER

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.