Create and manipulate array with asp classic

0

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

I need to do something 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?

    
asked by anonymous 14.12.2017 / 21:14

2 answers

4

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 through the values, you normally divide the dictionary into braces and values, and use the "Count" property to know how many items it has:

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
    
14.12.2017 / 21:30
1

You can use 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 it can be done like this:

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

To change some value, you must enter the key and the new value, for 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 template to exclude, eg:

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

to remove all items:

d.RemoveAll

14.12.2017 / 21:34