使用ColdFusion,我怎么显示在随机顺序数组中的元素? [英] Using coldfusion, how do I display the elements of an array in random order?

查看:93
本文介绍了使用ColdFusion,我怎么显示在随机顺序数组中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ColdFusion,我的目标是输出数组在一个完全随机的所有元素。我的数组这是从一个XML文件拉到6000项。

通常情况下,如果我没有要求他们在一个随机的顺序输出,我会做这样的:

 <&CFOUTPUT GT;
&所述; CFLOOP从=1=#arraylen(thestring)#索引=×>
#thestring [X]#< BR />
< / CFLOOP>
< / CFOUTPUT>

其结果是,我的琴弦显示在他们从XML文件时,拉顺序为:

  thestring [1]
thestring [2]
thestring [3]
thestring [4]

等。等所有的方式到thestring [6000]。

我想看到的是所有项目输出完全随机的顺序为:

  thestring [5]
thestring [22]
thestring [4301]
thestring [201]
thestring [2041]


解决方案

更新:这更多的是一个快速演示来证明一个较早的建议会工作的。但也有少数的其他建议比较精简。因此,我建议用其中之一吧。

根据您最新的更新,我认为没有理由雷蒙德的建议是行不通的。只要你保持跟踪已显示的元素。下面是一个简单的例子

 <&CFSCRIPT GT;
    //生成样品阵列
    yourArray = [];
    为(X = 1; X&下; = 20; X ++){
            arrayAppend(yourArray,项&放大器; X);
    }    //用于存储指数
    访问= {};
    为(X = 1; X&下; = arrayLen(yourArray); X ++){            //获得我们以前没有见过的随机指数
            做{
                    指数= randRange(1,arrayLen(yourArray));
            }
            而(structKeyExists(访问,索引));            //显示它
            WriteOutput(yourArray [指数]&安培;< BR>中);            //标志是访问,所以我们不要再次显示它
            参观[指数] =真;
    }
< / CFSCRIPT>

Using ColdFusion, my goal is to output all the elements of an array in a totally random order. My array has 6,000 items which are pulled from an XML file.

Usually, if I didn't require them to be output in a random order, I would do this:

<cfoutput>
<cfloop from="1" to="#arraylen(thestring)#" index="x">
#thestring[x]#<br/>
</cfloop>
</cfoutput>

The result is that my strings are displayed in the order they were when pulled from the XML file:

thestring[1]
thestring[2]
thestring[3]
thestring[4]

Etc. etc. all the way up to thestring[6000].

What I would like to see is all items output in totally random order:

thestring[5]
thestring[22]
thestring[4301]
thestring[201]
thestring[2041]

解决方案

Update: This was more of a quick demo to demonstrate an earlier suggestion would work . But a few of the other suggestions are more streamlined. So I would recommend using one of them instead.

Based on your latest update, I see no reason Raymond's suggestion would not work. As long as you keep track of the elements already displayed. Here is a simple example

<cfscript>
    // generate sample array
    yourArray = [];
    for (x = 1; x <= 20; x++) {
            arrayAppend(yourArray, "item "& x);
    }

    // stores used indices
    visited = {};
    for (x = 1; x <= arrayLen(yourArray); x++) {

            // get a random index we have not seen before
            do {
                    index = randRange(1, arrayLen(yourArray));
            }
            while (structKeyExists(visited, index));

            // display it
            WriteOutput(yourArray[index] &"<br>");

            // mark are "visited" so we do not display it again
            visited[index] = true;
    }
</cfscript>

这篇关于使用ColdFusion,我怎么显示在随机顺序数组中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆