冷聚变 Sorting/Assignment
ColdFusion Sorting/Assignment
我有两个列表:一个是动态的,基于学生的记录计数,另一个是学生……即 001,002,003 等和 123456(学生数据)。我需要一些帮助才能随机分配给学生其中一个数字。例如,如果我有 5 个学生(123456,234561 等),我需要能够随机分配 001,002 等给这些学生。至此,撞墙了。以下是我目前所拥有的:
<cfquery name="testjd" datasource="student">
SELECT SoonerID FROM dbo.CurrentStudentData
WHERE status = 'Student' OR status = 'JD'
</cfquery>
<cfset testList = valueList(testjd.soonerid)>
<cfset totalRecordsJd = #testjd.recordcount#>
<cfloop from="001" to="#totalRecordsJd#" index="i">
<cfset examNo = i>
<cfif len(i) is 1>
<cfset examNo = "00" & i>
<cfelseif len(i) is 2>
<cfset examNo = "0" & i>
</cfif>
<cfif not listFind(usedJDExams, examNo)>
#examNo#<!---is NOT being used!---><br/>
</cfif>
</cfloop>
这是格式化的评论。在你 运行 这个查询之后:
<cfquery name="testjd" datasource="student">
SELECT SoonerID FROM dbo.CurrentStudentData
WHERE status = 'Student' OR status = 'JD'
</cfquery>
这样做:
<cfset QueryAddColumn(testJd, "newcolumn", arrayNew(1))>
然后遍历查询并使用 QuerySetCell
将值分配给新列。
CF9 比后来的版本少了一点乐趣。我相信这应该可行(我的查询模型除外)。
https://trycf.com/gist/3667b4a650efe702981cb934cd325b08/acf?theme=monokai
首先,我创建了假查询。
<!---
Simple faked up query data. This is just demo data. I think this
queryNew() syntax was first available in CF10.
--->
<cfscript>
testjd = queryNew("SoonerID", "varchar", [
["123456"],
["564798"],
["147258"],
["369741"]
]);
</cfscript>
现在我已经有了需要测试的学生列表,我为这些测试创建了一个数字数组。
<!--- Create array of Available Exam Numbers --->
<cfset examNos = ArrayNew(1)>
<cfloop from=1 to=100 index="i">
<cfset examNos[i] = right('00' & i,3)>
</cfloop>
我们现在合并两组数据以获得分配给学生的考试编号。
<!--- Loop through the query and build random assignments --->
<cfloop query="#testjd#">
<!---Get random exam no.--->
<cfset examNo = examNos[randrange(1,arrayLen(examNos))]>
<!---Build struct of exam assignments--->
<cfset testAssignments[SoonerID] = examNo>
<!---Delete that num from exam array. No reuse.--->
<cfset blah = arrayDelete(examNos,examNo)>
</cfloop>
这给了我们
<cfoutput>
<cfloop collection="#testAssignments#" item="i">
For User #i#, the test is #testAssignments[i]#.<br>
</cfloop>
</cfoutput>
The unused tests are: <cfoutput>#ArrayToList(examNos)#</cfoutput>.
--------------------------------------------------------------------
For User 369741, the test is 054.
For User 147258, the test is 080.
For User 564798, the test is 066.
For User 123456, the test is 005.
The unused tests are:
001,002,003,004,006,007,008,009,010
,011,012,013,014,015,016,017,018,019,020
,021,022,023,024,025,026,027,028,029,030
,031,032,033,034,035,036,037,038,039,040
,041,042,043,044,045,046,047,048,049,050
,051,052,053,055,056,057,058,059,060
,061,062,063,064,065,067,068,069,070
,071,072,073,074,075,076,077,078,079
,081,082,083,084,085,086,087,088,089,090
,091,092,093,094,095,096,097,098,099,100.
OP 代码的一些代码审查说明:
1) 使用数组或结构比使用列表更容易。
2) cfloop from="001" to="#totalRecordsJd#"
: 来自“001”的字符串是您要与整数进行比较的字符串。 ColdFusion 会在后台将“001”转换为数字,这样它才能真正开始循环。注意预期的数据类型,并确保您按预期使用参数。
3) cfif len(i) is 1...
:首先,一次性构建此字符串的处理量较少,trim 它 - right('00' & i,3)
。其次(这是个人吹毛求疵),is
和 eq
本质上做同样的事情,但我一直发现将 is
应用于字符串式事物和 eq
到数字式的东西。
============================================= ========================
对于 CF10+,我会使用
https://trycf.com/gist/82694ff715fecd328c129b255c809183/acf2016?theme=monokai
<cfscript>
// Create array of random string assignments for ExamNo
examNos = [] ;
for(i=1; i lte 100;i++) {
arrayAppend(examNos,right('00'& i,3)) ;
}
///// Now do the work. ////
//Create a struct to hold final results
testAssignments = {} ;
// Loop through the query and build random assignments
for(row in testjd) {
// Get random exam no.
examNo = examNos[randrange(1,arrayLen(examNos))] ;
// Build struct of exam assignments
structInsert(testAssignments, row.SoonerID, examNo) ;
// Delete that num from exam array. No reuse.
arrayDelete(examNos,examNo) ;
}
</cfscript>
如果它是 small 查询,为什么不使用 NEWID() 随机排序记录(伪)?由于记录已经随机化,您可以使用 query.currentRow
构建 "examNo".
<cfquery name="testjd" datasource="student">
SELECT SoonerID
FROM CurrentStudentData
WHERE status IN ('Student', 'JD')
ORDER BY NewID()
</cfquery>
<cfoutput query="yourQuery">
#yourQuery.SoonerID# #NumberFormat(yourQuery.currentRow, "000000")#<br>
</cfoutput>
我有两个列表:一个是动态的,基于学生的记录计数,另一个是学生……即 001,002,003 等和 123456(学生数据)。我需要一些帮助才能随机分配给学生其中一个数字。例如,如果我有 5 个学生(123456,234561 等),我需要能够随机分配 001,002 等给这些学生。至此,撞墙了。以下是我目前所拥有的:
<cfquery name="testjd" datasource="student">
SELECT SoonerID FROM dbo.CurrentStudentData
WHERE status = 'Student' OR status = 'JD'
</cfquery>
<cfset testList = valueList(testjd.soonerid)>
<cfset totalRecordsJd = #testjd.recordcount#>
<cfloop from="001" to="#totalRecordsJd#" index="i">
<cfset examNo = i>
<cfif len(i) is 1>
<cfset examNo = "00" & i>
<cfelseif len(i) is 2>
<cfset examNo = "0" & i>
</cfif>
<cfif not listFind(usedJDExams, examNo)>
#examNo#<!---is NOT being used!---><br/>
</cfif>
</cfloop>
这是格式化的评论。在你 运行 这个查询之后:
<cfquery name="testjd" datasource="student">
SELECT SoonerID FROM dbo.CurrentStudentData
WHERE status = 'Student' OR status = 'JD'
</cfquery>
这样做:
<cfset QueryAddColumn(testJd, "newcolumn", arrayNew(1))>
然后遍历查询并使用 QuerySetCell
将值分配给新列。
CF9 比后来的版本少了一点乐趣。我相信这应该可行(我的查询模型除外)。
https://trycf.com/gist/3667b4a650efe702981cb934cd325b08/acf?theme=monokai
首先,我创建了假查询。
<!---
Simple faked up query data. This is just demo data. I think this
queryNew() syntax was first available in CF10.
--->
<cfscript>
testjd = queryNew("SoonerID", "varchar", [
["123456"],
["564798"],
["147258"],
["369741"]
]);
</cfscript>
现在我已经有了需要测试的学生列表,我为这些测试创建了一个数字数组。
<!--- Create array of Available Exam Numbers --->
<cfset examNos = ArrayNew(1)>
<cfloop from=1 to=100 index="i">
<cfset examNos[i] = right('00' & i,3)>
</cfloop>
我们现在合并两组数据以获得分配给学生的考试编号。
<!--- Loop through the query and build random assignments --->
<cfloop query="#testjd#">
<!---Get random exam no.--->
<cfset examNo = examNos[randrange(1,arrayLen(examNos))]>
<!---Build struct of exam assignments--->
<cfset testAssignments[SoonerID] = examNo>
<!---Delete that num from exam array. No reuse.--->
<cfset blah = arrayDelete(examNos,examNo)>
</cfloop>
这给了我们
<cfoutput>
<cfloop collection="#testAssignments#" item="i">
For User #i#, the test is #testAssignments[i]#.<br>
</cfloop>
</cfoutput>
The unused tests are: <cfoutput>#ArrayToList(examNos)#</cfoutput>.
--------------------------------------------------------------------
For User 369741, the test is 054.
For User 147258, the test is 080.
For User 564798, the test is 066.
For User 123456, the test is 005.
The unused tests are:
001,002,003,004,006,007,008,009,010
,011,012,013,014,015,016,017,018,019,020
,021,022,023,024,025,026,027,028,029,030
,031,032,033,034,035,036,037,038,039,040
,041,042,043,044,045,046,047,048,049,050
,051,052,053,055,056,057,058,059,060
,061,062,063,064,065,067,068,069,070
,071,072,073,074,075,076,077,078,079
,081,082,083,084,085,086,087,088,089,090
,091,092,093,094,095,096,097,098,099,100.
OP 代码的一些代码审查说明:
1) 使用数组或结构比使用列表更容易。
2) cfloop from="001" to="#totalRecordsJd#"
: 来自“001”的字符串是您要与整数进行比较的字符串。 ColdFusion 会在后台将“001”转换为数字,这样它才能真正开始循环。注意预期的数据类型,并确保您按预期使用参数。
3) cfif len(i) is 1...
:首先,一次性构建此字符串的处理量较少,trim 它 - right('00' & i,3)
。其次(这是个人吹毛求疵),is
和 eq
本质上做同样的事情,但我一直发现将 is
应用于字符串式事物和 eq
到数字式的东西。
============================================= ========================
对于 CF10+,我会使用
https://trycf.com/gist/82694ff715fecd328c129b255c809183/acf2016?theme=monokai
<cfscript>
// Create array of random string assignments for ExamNo
examNos = [] ;
for(i=1; i lte 100;i++) {
arrayAppend(examNos,right('00'& i,3)) ;
}
///// Now do the work. ////
//Create a struct to hold final results
testAssignments = {} ;
// Loop through the query and build random assignments
for(row in testjd) {
// Get random exam no.
examNo = examNos[randrange(1,arrayLen(examNos))] ;
// Build struct of exam assignments
structInsert(testAssignments, row.SoonerID, examNo) ;
// Delete that num from exam array. No reuse.
arrayDelete(examNos,examNo) ;
}
</cfscript>
如果它是 small 查询,为什么不使用 NEWID() 随机排序记录(伪)?由于记录已经随机化,您可以使用 query.currentRow
构建 "examNo".
<cfquery name="testjd" datasource="student">
SELECT SoonerID
FROM CurrentStudentData
WHERE status IN ('Student', 'JD')
ORDER BY NewID()
</cfquery>
<cfoutput query="yourQuery">
#yourQuery.SoonerID# #NumberFormat(yourQuery.currentRow, "000000")#<br>
</cfoutput>