如何在VBscript中混洗二维数组?

[英]How can I shuffle a bidimensional array in VBscript?


I need to shuffle a bidimensional array in vbscript, someone told me that I can use newid() in the sql query, but I'm not using sql server. I'm seeing over the web something about LBound and UBound, but don't understand very much. Can someone help me understand or tell me the best way to do it? Thanks in advance. This is my code so far.

我需要在vbscript中随机播放一个二维数组,有人告诉我,我可以在sql查询中使用newid(),但我没有使用sql server。我在网上看到有关LBound和UBound的一些内容,但不太了解。有人可以帮我理解或告诉我最好的方法吗?提前致谢。到目前为止这是我的代码。

sql = "SELECT * FROM Questions"

recordset.Open sql, connection

If Not recordset.EOF Then
    nQuestions = recordset.RecordCount
    questions = recordset.GetRows(nQuestions, 0)
    If IsArray(questions) Then
    (...)
    End If
End If

1 个解决方案

#1


1  

To answer the part of your question relating to UBound and LBound:

回答有关UBound和LBound的问题部分:

LBound and UBound are the lower and upper bounds of the array. You specify two arguments, the array name and the dimension that you want to count, for instance:

LBound和UBound是数组的下限和上限。您可以指定两个参数,即数组名称和要计数的维度,例如:

Dim myArr(10, 8)
Dim firstDim, secondDim
firstDim = UBound(myArr, 1)  ' The 1 here requests the size of the first dimension of the array
secondDim = UBound(myArr, 2) '... and this, the second.

Here, firstDim will hold the size of the first dimension of your array (i.e. 10), and secondDim the second (i.e. 8).

这里,firstDim将保持数组第一维的大小(即10),secondDim保持第二维(即8)的大小。

Obviously LBound works for the lowest dimension, though is only really used for arrays with a base other than the usual zero (possibly from COM objects).

显然LBound适用于最低维度,但实际上只用于具有除通常零之外的基数的数组(可能来自COM对象)。

-- EDIT --

- 编辑 -

To expand this further, you can get around this using a function in Access, but it does slow the process dramatically.

为了进一步扩展这一点,您可以使用Access中的函数解决此问题,但它确实会显着减慢该过程。

You'll need to create a query to select your data. Select the table that contains your questions. Drop all of the columns you need for your questions into the grid at the bottom. In the Field box in the right most column of the grid type something like:

您需要创建一个查询来选择您的数据。选择包含您问题的表格。将问题所需的所有列拖放到底部的网格中。在网格类型最右侧列的“字段”框中,类似于:

randomiser: Rnd([questionId])

(Obviously questionId being the unique id of the questions).

(显然问题是问题的唯一ID)。

This will give you a list of questions with a random number (from 0 to 1) in the last column. Now change the sort order for this column to Ascending, and you can also add a TOP clause to the query to give you a specific number of questions.

这将为您提供最后一列中随机数(从0到1)的问题列表。现在将此列的排序顺序更改为Ascending,您还可以向查询添加TOP子句,以便为您提供特定数量的问题。

Because an argument is specified for each row in the Rnd function it must execute every time, unlike simply putting Rnd(). Notice, also, if you run the query and click into one of the randomised fields the number changes.

因为为Rnd函数中的每一行指定了一个参数,所以它必须每次都执行,而不像简单地放入Rnd()。另请注意,如果您运行查询并单击其中一个随机字段,则数字会更改。

One thing to bear in mind here is that if you have an empty (NULL) value in one of the fields the sort will fail.

这里要记住的一件事是,如果在其中一个字段中有一个空(NULL)值,则排序将失败。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/08/18/733da8a2a276c9c17ca896388d1336f0.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号