写过PHP的都知道,其有个extract()非常方便,可以便捷的将字典转换为变量,当然到ASP中则要受限很多,特别是VBScript脚本,本文叙述的就是一种转换的思路,可以实现类似的功能。
下面我就直接提供ASP版本的extract代码吧:
'
' ASP/VBScript Dictionary extract
' Author: WangYe
' For more information please visit
'
' This code is distributed under the BSD license
'
' collection 集合或者字典,可以通过For Each访问的
' Request.Form 或者 Request.QueryString
' specified 指定必须存在的属性,假如该属性不存在,将自动创建一个
' prefix 每个属性的前缀修饰
' callback 对于集合或者字典的每个元素(key-value)的值进行函数调用
' 函数原型:
' Function filter(key, value)
' filter = value
' End If
' 最终值将以该函数返回的值为准
'
Function extract(collection, ByVal specified, prefix, callback)
Dim VarName, VarValue, DynObj, searchKey
specified = "," & Replace(specified, " ", "") & ","
Set DynObj = New DynamicObject
For Each key In collection
searchKey = "," & key & ","
If InStr(1, specified, searchKey, 1)>0 Then
specified = Replace(specified, searchKey, "")
If Left(specified, 1) <> "," Then
specified = "," & specified
End If
If Right(specified, 1) <> "," Then
specified = specified & ","
End If
End If
VarName = prefix & key
VarValue = collection(key)
If callback<>"" Then
VarValue = GetRef(callback)(key, VarValue)
End If