vbscript版的php extract()函數(shù)

字號:


    寫過php的都知道,其有個extract()非常方便,可以便捷的將字典轉(zhuǎn)換為變量,當(dāng)然到asp中則要受限很多,特別是vbscript腳本,本文敘述的就是一種轉(zhuǎn)換的思路,可以實(shí)現(xiàn)類似的功能。
    下面我就直接提供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  指定必須存在的屬性,假如該屬性不存在,將自動創(chuàng)建一個
    ' prefix     每個屬性的前綴修飾
    ' callback   對于集合或者字典的每個元素(key-value)的值進(jìn)行函數(shù)調(diào)用
    '            函數(shù)原型:
    '            function filter(key, value)
    '                filter = value
    '            end if
    '            最終值將以該函數(shù)返回的值為準(zhǔn)
    '
    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
            dynobj.add varname, varvalue, property_access_readonly
     next
        specified_array = split(specified, ,)
        dim i
        for i = lbound(specified_array) to ubound(specified_array)
            if specified_array(i)<> then
                dynobj.add prefix & specified_array(i), , _
      property_access_readonly
            end if
        next
        set extract = dynobj.getobject()
    end function
    再介紹下使用方法:
    代碼如下:
    dim query
    set query = extract(request.querystring, name,id, , )
        response.write query.name
        response.write query.id
    set query = nothing
    訪問包含上述代碼的asp頁面,在querystring(就是url問號后面的)包含name=wangye你將看到頁面輸出”wangye”,包含id=12的時候,將輸出”12″,當(dāng)然你也可以同時指定兩項(xiàng)。
    你可能發(fā)現(xiàn)當(dāng)你response.write輸出name和id之外key的時候,程序報(bào)錯了,因?yàn)橹付ǖ膶傩圆淮嬖?,?dāng)你在查詢字符串包含這個key的時候,程序又正常了,因?yàn)橛辛诉@個key就自動建立了屬性,所以又可以直接response.write了,如何避免呢?
    1. 通過extract()函數(shù)的specified參數(shù),該參數(shù)是個以逗號隔開key的字符串,你可以看到剛才示例代碼中運(yùn)用了這個特性,如果查詢字符串未包含相應(yīng)的key,但是你又使用了這個key,只要specified列表中有,就會自動建立值為空的屬性,所以就不會報(bào)錯啦。
    2. 通過返回對象的hasattr_方法進(jìn)行使用前判斷,這個方法可以判斷extract()函數(shù)返回的對象是否存在相應(yīng)的屬性,比如代碼有:
    代碼如下:
    dim query
    set query = extract(request.querystring, name,id, , )
    if query.hasattr_(job) then
        response.write job : & query.job
    end if
    set query = nothing
    這里job并不在我們的specified列表中,但是不帶查詢字串的直接訪問程序沒有報(bào)錯,因?yàn)槲覀兺ㄟ^hasattr_在使用前進(jìn)行判斷是否存在此屬性。
    3. 通過返回對象的getattr_方法進(jìn)行安全訪問,這個方法會在使用前判斷指定的屬性是否存在,如果不存在則用默認(rèn)值替代,詳細(xì)參考dynamicobject說明,比如代碼:
    代碼如下:
    dim query
    set query = extract(request.querystring, name,id, , )
        response.write job : & query.getattr_(job, no job)
    set query = nothing
    最后再介紹下filter的使用,extract()函數(shù)的filter參數(shù),指定的是另外一個函數(shù)名字符串,然后extract()將對每個值調(diào)用該函數(shù)進(jìn)行處理,比如過去有這樣的代碼:
    代碼如下:
    dim name, job, id
    name = trim(request.querystring(name))
    job = trim(request.querystring(job))
    id = clng(trim(request.querystring(id)))
    可以看到,我們每一次都調(diào)用了trim()函數(shù),重復(fù)的寫多次很麻煩,而且以后如果要改變相應(yīng)功能還要一個一個替換,通過filter參數(shù)我們可以這樣寫:
    '
    ' function filter(key, value)
    '   filter = trim(value)
    ' end function
    '
    function filter(key, value)
        on error resume next
        select case key
            case id ' 判斷id是否是數(shù)字
                if not isnumeric(value) then
                    exit function
                end if
                if clng(value)<1 then
                    exit function
                end if
        end select
        ' 最后記得讓函數(shù)返回值,該值在extract將被置為該返回值
        filter = trim(value)
        if err.number<>0 then
            filter =
        end if
    end function
    dim query
    set query = extract(request.querystring, name,id,job, , filter)
        response.write query.name
        response.write query.job
        response.write query.id
    set query = nothing
    剛才我們是以request.querystring為例子的,當(dāng)然你也可以使用request.form來實(shí)現(xiàn)表單處理