Create and manipulate array with classic Asp

Asked

Viewed 4,096 times

0

How to create, feed and read the array with key and value using Asp Classic?

I need to do something kind of like this.

$registro[] = array(
       "codigo"=> "$v",
       "municipio"=> "$v->municipio",
       "Rua"=> "$v",
       "Nome"=> "$v",
       "Sede"=> "$v",
       "Regional"=> "$v"
);

then I need something like foreach

Could someone help?

2 answers

5

To use keys and values in Classic ASP you have the Dictionary:

Set registro = Server.CreateObject("Scripting.Dictionary")
registro.Add "codigo", "12233"
registro.Add "municipio", "santo olavo"
...

To iterate on values, you normally divide the dictionary into keys and values, and use the "Count" property to know how many items you have:

chaves  = registro.Keys
valores = registro.Items

For i = 0 To registro.Count - 1 'Lembre-se do -1, pois começamos de zero
  chave = chaves(i)
  valor = valores(i)
  Response.Write( chave & " => " & valor & "<br>")
Next

Or even:

For each chave in registro.Keys
    Response.Write( chave & " => " & registro.Item(chave) & "<br>")
Next

1

The ASP Dictionary Object, the example below:

<%
    '//Criando o dicionário
    Dim d
    Set d = CreateObject("Scripting.Dictionary")

    '//Adicionando os itens
    d.Add "re","Red"
    d.Add "gr","Green"
    d.Add "bl","Blue"
    d.Add "pi","Pink"

    '//Recuperando e imprimindo valores
    dim a,c
    a = d.Keys
    c = d.items
    for i=0 to d.Count-1
        Response.Write(a(i) + " " + c(i))
        Response.Write("<br>")
    next
%>

If you still want to print with For each can be done like this:

For each item in d.Keys
    Response.Write(item + " " + d.Item(item))
    Response.Write("<br>")  
Next

and to change some value, must be informed the key and the new value, example:

if (d.Exists("re")) then '// verificando se a chave existe        
    d.Item("re") = "new valor" '// alterando valores
end if

this also follows the same model to exclude, example:

if (d.Exists("re")) then  '// verificando se a chave existe
    d.Remove("re") '// excluindo item 
end if

to remove all items:

d.RemoveAll

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.