Avatar billede ducks Nybegynder
07. januar 2004 - 05:44 Der er 4 kommentarer og
1 løsning

Array til at behandle mange variabler

Nu er jeg ikke så god til arrays, men hvis man har en masse variabler, tænkte jeg på om man på en smartere måde, end at først skrive en lang liste hvor de får en værdi, så en lang liste hvor man HTML encoder dem osv. Om man ikke kan lave det smartere ved at bruge array? Hvis det har nogen betydning har variablerne samme navn som den form de kommer fra, altså fx:

xx = Request.Form("xx")
yy = Request.Form("yy")
zz = Request.Form("zz")

osv.
Avatar billede powerade Nybegynder
07. januar 2004 - 08:39 #1
<%
'*******************************************************
'*    ASP 101 Sample Code - http://www.asp101.com/    *
'*                                                    *
'*  This code is made available as a service to our  *
'*      visitors and is provided strictly for the      *
'*              purpose of illustration.              *
'*                                                    *
'*      http://www.asp101.com/samples/license.asp      *
'*                                                    *
'* Please direct all inquiries to webmaster@asp101.com *
'*******************************************************
%>

<%
Dim arrNames(3)    ' Declare a static array with 4 elements
Dim arrAges()      ' Declare a dynamic array that we can resize
Dim arrColors      ' Declare a standard variable that we can
                    ' later assign an array to if we need to.
%>
<!--
This outer table is just so you don't end up having to scroll
so far down to get to the view source buttons.  It really has
nothing to do with the sample.
-->
<table border="1">
<tr>
<td valign="top">
<h3>Static Array</h3>
<%
' Let's start with our static array.  Static doesn't mean you
' can't change the values of the elements.  It simply means you
' can't change the number of elements in the array.  The values
' of the elements themselves can change as often as they need to.
arrNames(0) = "Susan"
arrNames(1) = "Andy"
arrNames(2) = "Fred"
arrNames(3) = "Kelly"

' Notice that even though I used the number 3 to dimension the
' array there are indeed four elements which I can insert values
' into.  This is because all arrays in VBScript are zero based
' meaning the first item has an index of zero.  Geeks think in
' binary and like to be different... what can I say?

' Display what our array currently contains. I wrapped this into
' a subroutine since I'm doing it a lot.  See the sub down at
' the bottom for implementation details.
ShowArrayInTable(arrNames)

' As I mentioned earlier... even with a static array I can
' reassign values if I need to.  I'm going to set the value of
' the second element to the value of the first element
' overwriting the existing value... effectively changing "Andy"
' to "Susan."  For good measure I'm also going to change the
' first element to "John" since I'm feeling left out.
arrNames(1) = arrNames(0)
arrNames(0) = "John"

' Here's what the array now contains:
ShowArrayInTable(arrNames)
%>
</td>
<td valign="top">
<h3>Dynamic Array</h3>
<%
' Now on to our dynamic array.  Before I can use a dynamic array
' I need to tell it how many elements I want to be able to put
' into it.  The ReDim command allows us to redimension an array
' to whatever size we need.  I'm setting it to 2 elements so I
' use an upper bound of 1.
ReDim arrAges(1)

arrAges(0) = 15
arrAges(1) = 20

' Show what our array currently contains:
ShowArrayInTable(arrAges)

' Now I'm going to ReDim the array again just to illustrate. I
' want to keep the existing data so I include the Preserve
' command.  If I didn't all the data already in the array would
' be lost.  You can also ReDim to change the number of
' dimensions in a dynamic array, but that's a little beyond the
' scope of this basic intro so here goes the simple resize up
' to 4 elements...
ReDim Preserve arrAges(3)

' Add another value:
arrAges(2) = 25
arrAges(3) = 30

' Once again show what our array currently contains:
ShowArrayInTable(arrAges)
%>
</td>
<td valign="top">
<h3>Array Using the Array Function</h3>
<%
' Now we come to an interesting point.  Since all variables
' in VBScript are really variants, you can assign an array to
' a variable that wasn't originally defined as one!
arrColors = Array("red", "green", "blue")

' Show the values in our new array:
ShowArrayInTable(arrColors)
%>
</td>
</tr>
</table>

<%
' Takes a 1 dimensional array and simply spits out its values
' in a table format with a note saying how big the array is.
Sub ShowArrayInTable(ArrayToShow)
    Dim I          ' Simple Looping Var
    Dim iArraySize  ' Var to store array size

    ' If you want to know how big an array is, you can use this
    ' to find out. This even works in VB where they don't have
    ' to be zero-based.  The LBound and UBound return the
    ' indecies of the lowest and highest array elements so to
    ' get the size we take the difference and add one since you
    ' can store a value at both end points.
    iArraySize = (UBound(ArrayToShow) - LBound(ArrayToShow)) + 1

    Response.Write "<p>The array has " & iArraySize _
        & " elements.  They are:</p>" & vbCrLf
   
    Response.Write "<table border=""1"">" & vbCrLf

    Response.Write "<thead>" & vbCrLf
    Response.Write "<tr>" & vbCrLf
    Response.Write "<th>Index</th>" & vbCrLf
    Response.Write "<th>Value</th>" & vbCrLf
    Response.Write "</tr>" & vbCrLf
    Response.Write "</thead>" & vbCrLf

    Response.Write "<tbody>" & vbCrLf
   
    ' Simple loop over a table outputting a row for each element
    For I = LBound(ArrayToShow) To UBound(ArrayToShow)
        Response.Write "<tr>" & vbCrLf
        ' Write out the index of the element we're currently on
        Response.Write "<td>" & I & "</td>" & vbCrLf
        ' Write out the value of the element we're currently on
        Response.Write "<td>" & ArrayToShow(I) & "</td>" & vbCrLf
        Response.Write "</tr>" & vbCrLf
    Next 'I
    Response.Write "</tbody>" & vbCrLf

    Response.Write "</table>" & vbCrLf
End Sub
%>
Avatar billede eagleeye Praktikant
07. januar 2004 - 08:44 #2
HVis du vil X antal varialbe ud. Kan det laves i stil med dette:


For Each Item In Request.Form
  strTemp = Item & "= Server.HTMLEncode(Request.Form(""" & Item & """))"
  Execute(strTemp)
Next



Hvis du har har input felterne xx, yy, zz så har du 3 variable med deres value HTMLencoded: xx, yy, zz

Lige som hvis du skrive det sådan her:
xx = server.htmlencode(Request.Form("xx"))
yy = server.htmlencode(Request.Form("yy"))
zz = server.htmlencode(Request.Form("zz"))
Avatar billede ducks Nybegynder
11. januar 2004 - 08:29 #3
Det var mere...

Temp = array(Request.Form("xx"), Request.Form("yy"), Request.Form("zz"))

i = 0
For Each Item In Temp
  Temp(i) = Server.HTMLEncode(Temp(i))
  Temp(i) = Trim(Temp(i))

  i = i + 1
Next

... jeg var ude efter
Avatar billede ducks Nybegynder
11. januar 2004 - 08:32 #4
Men.... eagleeye, ser lige at dit svar er jo egentlig reelt nok da jeg ikke skrev at der var et fast antal. Så vil du have pointene?
Avatar billede eagleeye Praktikant
11. januar 2004 - 12:46 #5
Ok, det kan vi godt ;)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester