2011-01-18 1 views

Répondre

3

Mise à jour La meilleure façon de le faire est avec le typeof() opérateur. C'est nouveau depuis la réponse, mais avec l'interprétation précoce des variables, l'ancienne méthode listée dans la réponse ne fonctionnera plus.

Un autre opérateur utile pour examiner vos données isnull()

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

La seule façon que je l'ai compris comment détecter le type de structure de données est en forçant à une chaîne, puis de vérifier si le résultat La chaîne de pointeur contient le mot 'array' ou 'hash'.

'une doublure'

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

myHashIsHash sera vrai/1

Exemple app construit pour montrer concept de

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; 
    } 
    } 
} 

Exemple app en action!

alt text

+0

C'est génial! Je vous remercie. On s'est mis à ça. Si votre variable est une chaîne et que le contenu de cette chaîne est 'ARRAY ...' ou 'HASH ...', cela ne fonctionne pas de manière fiable. Je vais vérifier s'il existe un moyen de détecter si une variable est une chaîne. –

Questions connexes