"Dave Anderson" <GT**********@s pammotel.com> wrote in message
news:u0******** ******@TK2MSFTN GP12.phx.gbl...
: Roland Hall wrote:
: >> Even less work:
: >> ...
: >
: > Not much.
: >
: > You're comparing that to:
: >
: > dim i : redim preserve Elements(Reques t.Form.Count - 1)
: > for i = 1 to Request.Form.Co unt
: > Elements(i - 1) = Request.Form.Ke y(i) & ": " & Request.Form.It em(i)
: > next
: >
: > ...and in another language.
:
: I am, indeed. And it is certainly less work to push an element onto an
array
: than to calculate the desired array size, resize that array, and keep
track
: of the array index (adjusting for the index offset, no less). The JScript
: Array happily accepts whatever you give it. Size and index come for free.
Terrific but the OP is using vbscript. I also could have responded in
Italian but he's using English.
: [... And now for the BIG ASIDE ...]
:
: In reality, I would not have used the construction in my example, as
JScript
: is far too interesting to stop there. A "step-through-the-collection"
: approach might be:
:
: for (var a=[],E=new Enumerator(Requ est.Form); !E.atEnd(); E.moveNext())
: a.push({Name:E. item(),Value:Re quest.Form(E.it em()).Item})
:
: This gives us an array of objects, each with [Name] and [Value]
properties.
: Now I can do interesting things, such as sort on [Name]:
Isn't that called an associative array? In VBScript we use a dictionary.
: a.sort(function (x,y){return x.Name<y.Name?-1:x.Name>y.Name ?1:0})
:
: Admittedly, this is of little advantage with a simple, always-available
: object like the Request.Form collection. It is considerably more powerful
: when used with recordsets, though:
:
: for (var Employees=[]; !RS.EOF; RS.MoveNext()) a.push(
: {
: ID: RS.Fields("Reco rdID").Value,
: Last: RS.Fields("Last Name").Value,
: First: RS.Fields("Firs tName").Value,
: SSN: RS.Fields("SSN" ).Value,
: Phone: RS.Fields("Phon eNumber").Value ,
: Address: RS.Fields("Post alAddress").Val ue,
: DOB: RS.Fields("Date OfBirth").Value ,
: Age: function(){retu rn Math.floor((new Date() - new
: Date(this.DOB))/31536000000)}
: }
: )
: RS.Close()
Why use recordset looping when you can use a 2-dimensional array?
: In this case, the array takes the place of a disconnected recordset -- it
is
: a group of objects with enumerated properties (and methods, if desired!).
I
: can step forward and backward through it, sort it, insert elements, delete
: others -- all without worrying about indices or whether I have the right
: kind of cursor.
I dump my recordset with GetRows into a 2-dimensional array and then close
and destroy the recordset and the connection. I find it to be pretty
powerful.
: Best of all, it leads to *sensible* tokens for the HMTL templates:
:
: <td><%=Employee s[i].ID%></td>
: <td><%=Employee s[i].Last%></td>
: <td><%=Employee s[i].Age()%></td>
I normally don't mix my HTML and server-side script. I usually have just
one opening <% and one closing %> and if I was writing out a table, I would
use GetString.
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center -
http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation -
http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library -
http://msdn.microsoft.com/library/default.asp