|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
help with Array
I have a script which is used to randomly allocate a teaching slot to a teacher.
What it does it to initially select which teachers are free for a time slot Then it counts how many are free for the time slot. Then it uses the randomize routine below to randomly allocate the lesson to a teacher. Since arrays start to count at zero, then I have to use the line below to reduce the count by 1 e.g. if the sql shows 'FREE_TCHR_CT' is 2, I have to reduce that to 1, so that the array can count from 0 to 1, instead of from 0 to 2, which will end up throwing out a fld_t value of nothing... Anyway, most of the time this process works, but I occasionally get this error: Microsoft VBScript runtime (0x800A0009) Subscript out of range: 'lrandom' /japan/users/save.asp, line 59 line 59 is: t_id = myArray(lrandom) I think this may only occur when FREE_TCHR_CT = 2 Can anyone suggest why this might be happening please? Thanks Jim code below: -------------------- sql002 = "SELECT fld_id, fld_t, (time_to_sec(fld_time)/1800) as periodnumber FROM appointments " sql002 = sql002 & "where fld_date = '"&d&"' AND (time_to_sec(fld_time)/1800) = '"&t&"' AND fld_cat = 2 GROUP BY fld_id" response.write "sql002<BR>" & sql002 & "<HR>" set rs002 = oConn.Execute(sql002) 'since there are free teacher(s) count the number of free teachers sql003 = "SELECT COUNT(fld_id) AS FREE_TCHR_CT FROM appointments where fld_date = '"&d&"' AND (time_to_sec(fld_time)/1800) = '"&t&"' AND fld_cat = 2 " response.write sql003 & "<HR>" set rs003 = oConn.Execute(sql003) FREE_TCHR_CT = rs003("FREE_TCHR_CT") response.write "FREE_TCHR_CT: " & FREE_TCHR_CT & "<BR>" 'if there is more than one teacher free then do the random thing if FREE_TCHR_CT > 1 then FREE_TCHR_CT = FREE_TCHR_CT-1 response.write "FREE_TCHR_CT rand: " & FREE_TCHR_CT & "<BR>" rs003.Close:set rs003=nothing 'response.write "<br><br>FREE_TCHR_CT: " & FREE_TCHR_CT & "<hR>" dim myArray ReDim myArray(FREE_TCHR_CT) randomize x = 0 do while not rs002.eof y = rs002("fld_id") myArray(x) = rs002("fld_t") x=x+1 rs002.MoveNext Loop rs002.close:set rs002 = nothing upperbound=ubound(myArray) lowerbound=0 lrandom = ((upperbound - lowerbound + 1) * rnd + lowerbound) t_id = myArray(lrandom) else t_id = rs002("fld_t") end if response.write "<hr>t_id: " & t_id & "<HR>" 'response.write "row_id: " & row_id & "<HR>" |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > help with Array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|