Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get individual elements from a loop

Status
Not open for further replies.

FreshJava

Programmer
Feb 20, 2002
65
0
0
US
How can I pull a single element from an array inside a function.

example:

dim varArr
varArr = Array (var1,var2,var3)
function getArr(arrNum)
dim i
i = LBound(varArr) to UBound(varArr)
next
arrNum = varArr(i)
getArr = arrNum
end function

response.write(getArr(1))

display should " be var2 "

the code is definatley wrong but would like to know how to do this.
Thanks

 
Not sure understand the question. Suppose varArr itself be global so that the inside of the function getArr is in its scope. It then would be as simple as this.
[tt]
function getArr(arrNum)
if arrNum>=lbound(varArr) and arrNum<=ubound(varArr) then
getArr = varArr(arrNum)
else
getArr="" 'out of range return empty or something you've to decide
end if
end function
[/tt]
 
sorry I was on my way to bed when I asked the question. what I am wanting is actually to pull a single element from an array that is inside a loop.

I left out the "for" the first time should look more like this

example:

dim one,two,three, varArr

one = request.Form("t1")
two = request.Form("t2")
three = request.Form("t3")
varArr = Array(one,two,three)

function getArr(arrNum)
dim i
for i = LBound(varArr) to UBound(varArr)
response.Write(varArr(i) & "<br />")
arrNum = varArr(2)
getArr = arrNum
next
end function


and what I am wanting is to display a particular element where ever I want on the page

example:
<table>
< tr>
<td> (getArr(1)"which would be element "two" </td>
</tr>
<tr>
<td>getArr(0)"which would be element "one"</td>
</tr>
</table>

I hope that clears the question up a little better

Thanks



 
I don't think I would change my getArr(). In the use of it you have to script correctly.

><td> (getArr(1)"which would be element "two" </td>
[tt]<td> <%= getArr(1) %> which would be element "two" </td>[/tt]

><td> (getArr(0)"which would be element "one" </td>
[tt]<td> <%= getArr(0) %> which would be element "one" </td>[/tt]

I have no idea what you want in display with unbalance parentheses and quotes. It doesn't matter.
 
Code:
<%
dim one,two,three, varArr  
one = "1"
two = "2"
three = "3"
varArr = Array(one,two,three)
%>
<html>
 <body>
  <table>
   <tr>
    <td>Element Two = <%= varArr(1)%></td>
   </tr>
   <tr>
    <td>Element One = <%= varArr(0)%></td>
   </tr>
  </table>
 </body>
</html>
 
I agree with Sheco, why even pass the array through a function if all the function is doing is just returning an element of the array.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top