Vetores e Arrays em CF e em ASP

No CF um vetor é uma variável normal.

<h1>List Loops</h1>

<h2>List Loop 1</h2>
<cfset numlist="1,2,3,4,5">
<cfloop index="num" list="#numlist#">
<cfoutput>#num#</cfoutput>
</cfloop>

<h2>List Loop 2</h2>
<cfset beatles="paul john ringo george">
<ul>
<cfloop index="beatle" list="#beatles#" delimiters=" ">
<li><cfoutput>#beatle#</cfoutput></li>
</cfloop>
</ul>

Abaixo a lista de funções CF específicar para vetores e arrays

Função
descrição
ListLen()
Determines the number of elements in a list.
ListFind()
Returns the index of the first list element in which a specified value occurs. Case-sensitive.
ListFindNoCase()
Determines the index of the first list element in which a specified value occurs. Case-insensitive.
ListContains()
Returns the index of the first list element that contains a specified substring. Case-sensitive.
ListContainsNoCase()
Returns the index of the first list element that contains a specified substring. Case-insensitive.
ListValueCount()
Counts instances of a specified value in a list. The search is case-sensitive.
ListValueCountNoCase()
Counts instances of a specified value in a list. The search is case-insensitive.
ListDeleteAt()
Returns a copy of the list without the specified element.
ListGetAt()
Gets a list element at a specified position.
ListSetAt()
Replaces the contents of a list element.
ListSort()
Sorts list elements according to a sort type and sort order.
ListAppend()
Appends a list or element to a list.
ListPrepend()
Inserts an element at the beginning of a list.
ListInsertAt()
Inserts an element in a list.
ListFirst()
Gets the first element of a list.
ListRest()
Gets a list, without its first element.
ListLast()
Gets the last element of a list.
ArrayToList()
Converts a one-dimensional array to a list.
ListToArray()
Converts a list to a one-dimensional array.
ReplaceList()
Replaces occurrences of the elements from a delimited list in a string with corresponding elements from another delimited list. The search is case-sensitive.
ListChangeDelims()
Changes a list delimiter.
ListQualify()
Inserts a string at the beginning and end of list elements.
No ASP para montar vetor podemos fazer da seguinte maneira:

<% Dim MeuArray(3)
MeuArray(0)=Date
MeuArray(1)="Lineu Antonio de Lima Santos"
MeuArray(2)=12.45
MeuArray(3)=Now
%>

Observe que o valor armazenado em cada elemento de um array pode ser de um subtipo diferente dos demais. Outra observação importante: arrays têm que ser declarados explicitamente.

Arrays não são limitados para uma única dimensão. Em VBScript pode-se declarar Arrays de até 60 dimensões.

Construindo uma matriz:

<% Dim MeuArray(1,1)
MeuArray(0,0)= "Lineu Antonio de Lima Santos"
MeuArray(0,1)=2225240
MeuArray(1,0)= "Universidade Federal do Piauí"
MeuArray(1,1)=2172000
%>

Pode-se declarar um array cujo tamanho é alterado durante a interpretação do script. Para tal, basta declarar o array com Dim sem informar a quantidade de elementos. Depois deve-se utilizar a declaração ReDim para determinar o número de elementos. Caso seja necessário redimensionar o array, utiliza-se novamente a declaração ReDim. Se houver a necessidade de preservar o conteúdo do array a ser redimensionado, utiliza-se a declaração ReDim Preserve.

Sem Preserve <BR>
<% Dim MeuArray()
ReDim MeuArray(1)
MeuArray(0)= "Lineu Antonio de Lima Santos"
MeuArray(1)=12.95
ReDim MeuArray(2)
MeuArray(2)=9090
%>

<B>Posição 0</B> = <%=MeuArray(0)%><BR>
<B>Posição 1</B> = <%=MeuArray(1)%><BR>
<B>Posição 2</B> = <%=MeuArray(2)%><BR>
<HR>
Com Preserve <BR>
<% ReDim MeuArray(1)
MeuArray(0)= "Lineu Antonio de Lima Santos"
MeuArray(1)=12.95
ReDim Preserve MeuArray(2)
MeuArray(2)=0909
%>
<B>Posição 0</B> = <%=MeuArray(0)%><BR>
<B>Posição 1</B> = <%=MeuArray(1)%><BR>
<B>Posição 2</B> = <%=MeuArray(2)%>

Para correr um vetor em ASP usamos as funções LBound() e UBound().

<script type="text/vbscript">
Dim food(2,3)
food(0,0)="Apple"
food(0,1)="Banana"
food(0,2)="Orange"
food(0,3)="Lemon"
food(1,0)="Pizza"
food(1,1)="Hamburger"
food(1,2)="Spaghetti"
food(1,3)="Meatloaf"
food(2,0)="Cake"
food(2,1)="Cookie"
food(2,2)="Icecream"
food(2,3)="Chocolate"
document.write(LBound(food,1) & "<br />")
document.write(UBound(food,1) & "<br />")
document.write(LBound(food,2) & "<br />")
document.write(UBound(food,2) & "<br />")

</script>

Comentários

Postagens mais visitadas deste blog

Criando uma Variável em ColdFusion e em ASP

Definindo Valores Padrões para Variáveis

O que é ColdFusion? Para que serve?