2011-01-18 5 views

答えて

3

更新 これを行うための最善の方法は、typeof演算()演算子です。これは答えから新しいですが、変数の初期の解釈では、答えに挙げられている古い方法はもはや機能しません。

あなたのデータを調べるための別の有用なオペレータは、ISNULL()

myHash.typeof() => "hash" 
myArray.typeof() => "array" 
... 
2

私はデータ構造タイプを検出する方法を考え出した唯一の方法は、得られたかどうかを確認するための文字列に強制変換してから確認することですポインタ文字列に 'array'または 'hash'という単語が含まれています。

'ワンライナー'

myHashIsHash = "#{myHash}".match(re/hash/gi); 

myHashIsHashが

1/trueになります例アプリが動作中の概念

ruleset a60x547 { 
    meta { 
    name "detect-array-or-hash" 
    description << 
     detect-array-or-hash 
    >> 
    author "Mike Grace" 
    logging on 
    } 

    global { 
    myHash = { 
     "asking":"Mike Farmer", 
     "question":"detect type" 
    }; 
    myArray = [0,1,2,3]; 
    } 

    rule detect_types { 
    select when pageview ".*" 
    pre { 
     myHashIsArray = "#{myHash}".match(re/array/gi); 
     myHashIsHash = "#{myHash}".match(re/hash/gi); 
     myArrayIsArray = "#{myArray}".match(re/array/gi); 
     myArrayIsHash = "#{myArray}".match(re/hash/gi); 

     hashAsString = "#{myHash}"; 
     arrayAsString = "#{myArray}"; 
    } 
    { 
     notify("hash as string",hashAsString) with sticky = true; 
     notify("array as string",arrayAsString) with sticky = true; 

     notify("hash is array",myHashIsArray) with sticky = true; 
     notify("hash is hash",myHashIsHash) with sticky = true; 
     notify("array is array",myArrayIsArray) with sticky = true; 
     notify("array is hash",myArrayIsHash) with sticky = true; 
    } 
    } 
} 

例のアプリを証明するために構築されました!

alt text

+0

これは素晴らしいです!ありがとうございました。これに1つのつまらなさ。変数が文字列で、その文字列の内容が 'ARRAY ...'または 'HASH ...'の場合、これは確実に機能しません。変数が文字列かどうかを検出する方法があるかどうかを調べます。 –

関連する問題