Create and manipulate arrays with ASP classic

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 anyone help?

Author: novic, 2017-12-14

2 answers

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

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

To iterate on the values, you usually break the dictionary into Keys 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
 5
Author: Bacco, 2017-12-14 20:42:26

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

And to change some value, the key and the new value must be entered, 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 delete, 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:

 1
Author: novic, 2017-12-14 20:56:10