ho scritto, per una classe che pubblico in giornata, tre funzioni utili per vari utilizzi
uno di questi è raggruppare argomenti in un'unica stringa per far si di avere un argomento di una funzione che possa avere più valori e quindi anche valori opzionali
(purtroppo l'overload in ASP è fantascienza)
vediamo un esempio pratico
l'output a schermo ècodice:<% '/***********************************************************************************/ function parseOption(byval columnoptions, columnoption) dim reg Set Reg = New RegExp Reg.Global = True Reg.Ignorecase = True dim Matches,return,value return = "" Reg.pattern = ":" & "(" & columnoption & ")[ ]*=[ ]*((?:,(?![ ]*:" & ")|[^,])+)" if reg.test(columnoptions) then set Matches = reg.Execute(columnoptions) value = trim(Matches(0).submatches(1)) if isnumeric(value) then value = clng(value) if value = "true" then value = true if value = "false" then value = false return = value end if parseOption = return end function function parseOptions(byval columnoptions) dim reg Set Reg = New RegExp Reg.Global = True Reg.Ignorecase = True dim Matches,match,return,value set return = CreateObject("Scripting.Dictionary") return.CompareMode = 1 Reg.pattern = ":" & "([a-z0-9_-]+)[ ]*=[ ]*((?:,(?![ ]*:" & ")|[^,])+)" if reg.test(columnoptions) then set Matches = reg.Execute(columnoptions) for each match in matches value = trim(match.submatches(1)) if isnumeric(value) then value = clng(value) if value = "true" then value = true if value = "false" then value = false return(trim(match.submatches(0))) = value next end if set parseOptions = return end function 'value = a, values = "|a|b|c|d|" => true function inValue(value,values) inValue = false if instr(values,"|" & value & "|") > 0 then inValue = true end function '/***********************************************************************************/ 'ESEMPIO dim a, aa, b, options options = ":test=ciao,:numero=52,:booleana=false" response.write "opzioni: """ & options & """ " set aa = parseOptions(options) b = parseOption(options,"test") for each a in aa response.write "" & a & " vale " & aa(a) & " " next response.write "b (che cerca 'test') vale " & b & " " response.write "test è uno di questi valori ""a,b,c"" : " & inValue(aa("test"),"|a|b|c|") & " " response.write "test è uno di questi valori ""ciao,sono,test"" : " & inValue(aa("test"),"|ciao|sono|test|") %>
in pratica prende i valori e li mette in un dictionary.codice:opzioni: ":test=ciao,:numero=52,:booleana=false" test vale ciao numero vale 52 booleana vale Falso b (che cerca 'test') vale ciao test è uno di questi valori "a,b,c" : Falso test è uno di questi valori "ciao,sono,test" : Vero
se il campo cercato non esiste penso restituisce "" o null, cmq con un trim(value&"") siete sicuri che il risultato se vuoto sia sempre ""
io nelle funzioni lo uso così
può venire utile anche in altri casicodice:function miafunc(argomento1, options) dim ioptions set ioptions = parseOptions(options) 'a questo punto dentro ioptions ho disponibili tutti i valori passati 'secondo la sintassi ":nome=valore,:nome2=valore2" 'per accedere al valore di nome scrivo semplicemente response.write ioptions("nome") end function
ovviamente solo se in diversi casi passate diversi valori e non volete scrivere funzioni differenti o argomenti flag
se avete commenti sono qui
(ho diviso le stringhe del pattern nello script perché vbullettin ci metteva dei simpatici ma poco funzionali smiles)
cià


Rispondi quotando
)
ghghgh
