PDA

View Full Version : array loop


Freon22
03-05-2004, 09:35 PM
Hi, All
Well I have found myself with another problem. Here it is I have this array and I am trying to count 1 for each number 5 that is in this array. I know that the number 5 is in it two times but I cann't loop through it. Any help would be great, heres the code.

Dim w5
w5 = 0
Do While Wlevel(6) = 5
w5 = w5 + 1
Loop

I have also tryed

Dim w5, w
w5 = 0
For w = 0 to 6
Do While Wlevel(w) = 5
w5 = w5 + 1
Loop

and this

Dim w5, w
w5 = 0
For w = 0 to 6
For Each 5 In Wlevel(w)
w5 = w5 + 1
Next

I know what it is I am doing it every way but the right way :-)
Please help, Thanks

DCElliott
03-06-2004, 06:25 AM
Sorry what language is this, I don't recognize the syntax?

Here is the general principle: Initialize your index variable for the array and your count variable for the hits where the array member equals 5. (I take it that Wlevel is your array)
You then loop from 0 to the number of array elements. (You need to check on your language here - many arrays start at 0 (for example Javascript) so the array index of the last member is the total members - 1)
Have an if statement that tests for Wlevel(index) = 5 and if true increment the variable counting 5s by 1In some languages it would look like:
count5=0
FOR index=1 TO ALEN(Wlevel)
IF Wlevel(index)=5
count5=count5+1
ENDIF
ENDFORALEN would be a function that gives the length of an array.

DE

MikeParent
03-06-2004, 06:53 AM
Your syntax is a little off Freon... The Do While doesnt do what you think it does :-)

The loop would look a bit more like what DC posted.


count5=0
For i = 0 to UBound(WLevel,1)
If WLevel(i) = 5 Then
count5=count5+1
End If
Next


OR


count5=0
For Each objItem in WLevel
If objItem = 5 Then
count5=count5+1
End If
Next


DC the language is VBScript in which:

Arrays are 0-based (first element is ArrayName(0) )
UBound gets the array size in the given dimension (which ironically is 1-based)

Edit: Spelling

Freon22
03-07-2004, 09:14 AM
Thanks DC and Mike

Mike you are right I don't real know how to use loops. Working on learning were and how to put them into a page. Learning vbscript from books leaves a lot out, that is why I am glad that there are people like you guys that are willing to help other learn the ends and outs. Again thanks
I do have a question that has been troubling me. Lets say that you have a db connection open and you are reading and writing to the db on this page. But you have a "If Then" statement that would move the visiter to another page if something is not meet. The db connection was opened at the start of the page should you close the connection before you redirected them or will the connection close itself when they move from the page? I know that you should close all connection as soon as you can on a page, but if you move them before the page finishes?

MikeParent
03-07-2004, 11:01 AM
Very fine question. What I usually do is just set a variable instead


IF something Then
blnRedirect = vbTrue
End If

more code including closing db connection

If blnRedirect Then
Response.Redirect "blah.asp"
End If

Freon22
03-07-2004, 02:04 PM
Thanks I will be able to use that else were. But on the page that I was thinking about it works like this. Through a form they are send to a page that compairs the information with a database. If the infromation is ok then it write the information to a table in the database and sends them to the next page, if not then it sends them back to try again. Your code does send them back but it also still write the information to the table. Would a Response.End work to stop the rest of the code from running. Here is some of my code.

'*** Opening up the Database
Dim objConn, strSQL
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
"DBQ=" & Server.MapPath("db/newbie.mdb")
objConn.Open

Dim Aweapons(6)
Aweapons(0) = Ahp1name
Aweapons(1) = Ahp2name
Aweapons(2) = Ahp3name
Aweapons(3) = Ahp4name
Aweapons(4) = Ahp5name
Aweapons(5) = Ahp6name
Aweapons(6) = Ahp7name

strSQL = "SELECT Weaponname, Shield, Armor, Accuracy, intlevel FROM Weapons WHERE Weaponname IN "
strSQL = strSQL & "('" & Join(Aweapons,"','") & "')"

'*** Opening up the Recordset--> Getting the first set of weapons from the database ***
Dim objRS, wname, w
Set objRS = Server.CreateObject ("ADODB.Recordset")
objRS.Open strSQL, objConn

Dim Ahpname(6), Ahpshield(6), Ahparmor(6), Ahpaccuracy(6), Wlevel(6)

DO Until objRS.EOF
wname = objRS("Weaponname")
For w = 0 To 6
If wname = Aweapons(w) Then
Ahpname(w) = objRS("Weaponname")
Ahpshield(w) = objRS("Shield")
Ahparmor(w) = objRS("Armor")
Ahpaccuracy(w) = objRS("Accuracy")
Wlevel(w) = objRS("intlevel")
End If
Next
objRS.MoveNext
Loop
objRS.Close
Set objRS = Nothing

'Checking to make sure the player didn't pick more then one level 5 weapon
'This is where I put your first code it works great
Dim count5, objItem, blnRedirect
count5 = 0
For Each objItem in WLevel
If objItem = 5 Then
count5 = count5 + 1
End If
Next
'This is where I started your next code
IF count5 > 1 Then
blnRedirect = vbTrue
End If
'There is more coding from here on that write information to the database. after the code is finished the connection is closed and the other part of your code ether sends them to the next page or sends them back. It all work good but I don't want it to write to the database if the information is not right. This is just a game and I thought it would be a good way to learn vbscript. I'm the type that learns by doing. Reading is great but if you don't use it and see what you can and cann't do with it then you lose it. Thanks for taking the time to look this over.

MikeParent
03-07-2004, 05:07 PM
Ah, seeing your code in greater context helps a lot... :-)

after your level 5 weapons check try this:

If count5 > 1 Then
blnRedirect= vbTrue
Else
'write database stuff
End If
'close database
If blnRedirect Then
Response.Redirect "somepage.asp"
End If



If you want to do a conditional redirect (one page if everything is OK or a different one if not) then instead of setting the blnredirect to true just do this instead:


If count5 > 1 Then
strRedirectPage="errorpage.asp"
Else
strRedirectPage = "successpage.asp"
'write database stuff
End If

'close database

Response.Redirect strRedirectPage

Freon22
03-08-2004, 12:36 PM
Thanks, Mike
Don't know why I didn't think about the Else. That way the rest of the code will not run unless the Else is called.
Thanks Your a Life Saver

MikeParent
03-08-2004, 03:36 PM
No prob - I have done a *lot* of ASP development - I cant do css like DC or PHP like DBindel (where is he these days anyhow), but I knows me some ASP sure nuff :-)

aishi
02-09-2005, 05:54 PM
good day! im having problem on incrementing the array index. this script should give me the prime numbers between 5 to 20. it doesnt work. pls help!


testno = 5
endno = 20
dim primeno(100)
primeno(0) = 3


for testno = 5 to endno

do while primeno(counter) <> ""
x = testno/primeno(counter)
x = x - int(x)
if x = 0 then
testno = testno + 2
else
counter = counter + 1
if primeno(counter) = "" then
primeno(counter) = testno
testno = testno + 2
else
' do nothing
end if
end if

loop


next

Freon22
02-11-2005, 10:12 AM
I want to say I had fun with your code. But was unable to get it to work, So I did what I always do I searched the net looking for a code that will do what I want it to do. It looks like you want to print out only the prime numbers between to given numbers. I looked but was unable to find a code that did what I think you want to do. So I then looked to see if there was a code that did something close to what you want, that could be modified to something that will work. I did find a very good function that takes a number and see if it is a prime and returns a true if it is and a false if not. I then modified it to print out only the prime numbers that are feed to it. I then put a simple loop so I could feed a serials of numbers to it. Anyway here is the code that I came up with, I hope it does want you want. The code works very good just make sure you copy all of the function, then you can make any type of loop that you want. I tested it with a large amount of numbers and as far as I can tell it only returns and prints the prime ones.

<%

Dim firstnumber, lastnumber, icount, number
Function numberIsPrime(number)
Dim squarenumber, indexnumber
numberIsPrime = False
if number < 2 Then exit function
if number mod 2 = 0 Then exit function
squarenumber = Sqr(number)
For indexnumber = 3 to squarenumber step 2
if number mod indexnumber = 0 Then exit function
next
numberIsPrime = True
if not numberIsPrime = False Then
Response.Write(number & "<br>")
end if
End Function

firstnumber = 5
lastnumber = 20
number = firstnumber
For icount = firstnumber to lastnumber
numberIsPrime number
number = number + 1
next

%>