在给定键值的visual basic数组中查找值

时间:2014-02-25 14:59:26

标签: arrays multidimensional-array

我正在创建一个页面,在每个页面上显示来自15个不同赞助商的4个赞助商广告。广告的加权频率应根据赞助商的价值加权。我已经在php中启动了类似的功能,但似乎无法在ASP中获得它

这就是我要做的事情: 1.创建一系列赞助商,其中一个“列”代表赞助商的价值 2.总结阵列中赞助商的价值 3.使用总计为数组中的每个赞助商创建一个权重百分比,并将此百分比添加为数组中的附加“列” 4.创建一个新的数组,根据权重重复赞助商值

在ASP中,我最终创建了一个三维数组,该数组根据其权重复制了我的初始Sponsors数据数组。它似乎工作 - 当我遍历我的100行newSponsors数组时,我可以写出数组的值。

newSponsors(n,x,i)=赞助商(x,i)

然后我将100个中的4个数字随机化,为我的3维数组创建关键字。我坚持要比较它们:

SponsorWeight = Sponsors(5,i)

Dim newSponsors(100,5,15)

for i = 0 to SponsorTotalCt
SponsorWeight = Sponsors(5,i)
for w = 1 to SponsorWeight
    for x = 0 to 5
        newSponsors(n,x,i) = Sponsors(x,i)
        value = Sponsors(x,i)
        response.write("Value = " & value & "<br>")
    next
    n = n + 1
next
x = 0
next

所以说我的Randomizer在100:17,9,88和27中生成4个随机数。我需要创建一个if语句:

if newSponsors(17,2,i) = newSponsors(9,2,i)
  pass = false

但我不知道我的价值是什么。我怎么得到它?

1 个答案:

答案 0 :(得分:0)

嗯,花了一天半的时间,但我终于想出了这个有效的代码 - 可能不是最有效的方法,但它确实起到了作用:

i = 0
For i = 0 to UBound(Sponsors, 2)
SponsorVal = Sponsors (2,i)
SponsorWeight = Round(SponsorVal/SponsorTotalAmt * 100,0)
Sponsors (5,i) = SponsorWeight

Next

'CREATE NEW ARRAY OF 100 TO RANDOMIZE AND WEIGHT
' n = key in 100 array
' x = key in Sponsors array
' i = row in Sponsors array
' w = weight in Sponsor array

Dim n
Dim x

n = 0
x = 0
i = 0
w = 1
SponsorWeight = Sponsors(5,i)

Dim newSponsors(100,15)

for i = 0 to SponsorTotalCt
SponsorWeight = Sponsors(5,i)
for w = 1 to SponsorWeight
    value = i
    newSponsors(n,0) = value
    response.write(value)
    n = n + 1
next
x = 0
next


' RANDOMIZE A VALUE FROM newSponsors

intHighNumber = n
intLowNumber = 1

pass = "false"

Dim spot(4)

For i = 1 to 4
    Randomize
    spot(i)= Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
Next

spot1 = spot(1)
spot2 = spot(2)
spot3 = spot(3)
spot4 = spot(4)

Sponsor1 = newSponsors(spot1,0)
Sponsor2 = newSponsors(spot2,0)
Sponsor3 = newSponsors(spot3,0)
Sponsor4 = newSponsors(spot4,0)

'MAKE SURE THAT THERE ARE NO DUPLICATE SPONSORS IN THE 4 RANDOMLY SELECTED

do 

如果newSponsors(spot1,0)&lt;&gt; newSponsors(spot2,0)AND newSponsors(spot1,0)&lt;&gt; newSponsors(spot3,0)AND newSponsors(spot1,0)&lt;&gt; newSponsors(spot4,0)AND newSponsors(spot2,0)&lt;&gt; newSponsors(spot3,0)AND newSponsors(spot2,0)&lt;&gt; ; newSponsors(spot4,0)AND newSponsors(spot3,0)&lt;&gt; newSponsors(spot4,0)然后         pass =“true”     其他         pass =“false”         对于i = 1到4             随机化             spot(i)= Int((intHighNumber - intLowNumber + 1)* Rnd + intLowNumber)             spot1 = spot(1)             spot2 = spot(2)             spot3 = spot(3)             spot4 = spot(4)             Sponsor1 = newSponsors(spot1,0)             Sponsor2 = newSponsors(spot2,0)             Sponsor3 = newSponsors(spot3,0)             Sponsor4 = newSponsors(spot4,0)         下一个     万一     Loop Until pass =“true”

response.write("Pass: " & pass & "<br>")
Response.write(Sponsor1 & "<br>" & Sponsor2 & "<br>" & Sponsor3 & "<br>" & Sponsor4)

'CALL THE APPROPRIATE VALUE IN THE SPONSORS ARRAY BASED ON THE NEWSPONSORS ARRAY
Response.write("<br><br>" & Sponsors(Sponsor1,4))

希望有人觉得它有用!