telophase: (gojyo screw you // yomigaere)
telophase ([personal profile] telophase) wrote2009-04-09 11:28 am
Entry tags:

aaaaaaaaaaaaaaaargh! VBScript

I've been wrestling with non-repeating random number* generator code all morning, and all the code samples I find don't work - every time they declare a variable AS something, the code dies. I can't figure out if it's because I'm doing something wrong, or if the version of VBScript I'm using doesn't support that.



For example, this code which I snagged somewhere, claims it shuffles an array of numbers from 1 to 10. But I get the error


Microsoft VBScript compilation error '800a0401'

Expected end of statement

/Surveys/2009-DeckTest.asp, line 16

Dim i As Long
------^


where it croaks at the As Long, which is the same thing with every other piece of code I've swiped - As Long, As Collection, As Integer. Aaaargh! It really doesn't help that I don't know enough about this stupid language.


Private Sub Form_Load()
Dim i As Long
Dim r As Long
Dim t As Long
Dim a(1 To 10) As Long
Randomize
'set up the array
For i = 1 To 10
a(i) = i
Next
'shuffle the array
For i = 1 To 10
r = Int(Rnd * 10) + 1 'get random value
'ensure r<>i
Do While r = i
r = Int(Rnd * 10) + 1
Loop
'swap the array members
t = a(i)
a(i) = a(r)
a(r) = t
Next
'output
For i = 1 To 10
Debug.Print a(i)
Next
End Sub


Context is that I am writing a survey app which, when someone agrees to answer a question for us, will grab the active questions from the database and shuffle them, then present the first one. After the patron answers that, it asks if they'd be willing to answer another. If so, then it grabs the next one in the array. Lather, rise, repeat. At the end of the array, it gives a polite message that there are no more questions to answer at this time. I need for questions not to repeat, and for them to be presented (pseudo-)randomly, so that if we're asking similar questions, we don't get biased answers because certain ones always appear first (if I'm explaining that correctly).








--
* Yes, I know, non-repeating makes it technically not random.

[identity profile] jarodrussell.livejournal.com 2009-04-09 04:47 pm (UTC)(link)
I didn't know you could declare types in ASP.

[identity profile] telophase.livejournal.com 2009-04-09 05:03 pm (UTC)(link)
You can do it in VBScript, it seems. There's plenty of examples And I'm even hazy on exactly what ASP *is*, as it seems to be something you can access or do with two other languages.

Gah, I hate this - what I know of coding languages is what I've taught myself, and therefore I only know what's come up as I've needed it, and when I hit something new to me, it amounts to miles of frustration over what other people view as basic stuff.

I'm pretty sure I can do this in PHP, even, but unfortunately the server I need to do this on doesn't have it.

ETA: And now I'm thinking I'm looking in the slightly wrong direction, and should be looking for ways to randomly sort arrays. Harrumph.

[identity profile] jarodrussell.livejournal.com 2009-04-09 05:45 pm (UTC)(link)
http://cassandra.wccs.edu/random.asp

http://cassandra.wccs.edu/random.asp.txt

I think that might be what you need.

[identity profile] telophase.livejournal.com 2009-04-09 06:00 pm (UTC)(link)
Thank you! ~♥

I shall look at that straghtaway.

[identity profile] ukoku.livejournal.com 2009-04-09 05:56 pm (UTC)(link)
That error code is "Expected end of statement". It means that somewhere in that script, there's a screwed statement....

I haven't worked with VB in ages, but I'll take a glance.

EDIT: Also, even though it tells you "where" the script screws up, it's always wrong and stupid. Just FYI.

[identity profile] telophase.livejournal.com 2009-04-09 05:57 pm (UTC)(link)
It's happening in every single script I find*, choking at that exact point no matter *what* type is declared. For some reason, it doesn't like declaring types.



* As my normal method for teaching myself something is to find other scripts, then monkey with them until I can see what changes cause what.

ETA: But it does have a terrifying consistency - I have tried about ten different scripts, all of which declare a type, all of which screw up exactly there.

[livejournal.com profile] jarodrussell's given me a script that may work, however, so no need to exert yourself unless the puzzle intrigues you. :) Thans!

[identity profile] ukoku.livejournal.com 2009-04-09 06:10 pm (UTC)(link)
Nah, I'm interested now, so I'm going to fiddle. Lol.

One thing I think I've found, is that you need a line that says "Start:" before the "Randomize", but I'm not sure if that's it.

(and now it's tellin gme I'm out of stack space....)

(Good luck with the other script, though.)