deparei-me com uma situação interessante.
Vejam isto:
aMatriz := {'a','b','c'}
? ascan(aMatriz,'b') // Retorna 2 Ok
? ascan(aMatriz,'') // Retorna 1 - ERRO
O comportamento relativo à segunda pesquisa está correto? Por quê?
MarceloG
Moderador: Moderadores



Código: Selecionar todos
Function Main
aMatriz := {0,'b','c'}
? ascan(aMatriz,'b') // Retorna 2 Ok
? ascan(aMatriz,"") // Retorna 2 - Ok
? ascan(aMatriz,'d') // Retorna 0 - Ok
inkey(0)
Return
Código: Selecionar todos
? AScan(aMatriz,{|x|valtype(x)="C" .and. x != ""})
Código: Selecionar todos
AScan(aMatriz,{|x|Empty(x)})
ou
AScan(aMatriz,{|x|x==""})
ou
AScan(aMatriz,{|x|""=x})Código: Selecionar todos
AScan(aMatriz,"")AScan()
Searches a value in an array beginning with the first element.
Syntax
Ascan( <aArray> , ;
<xbSearch>, ;
[<nStart>], ;
[<nCount>], ;
[<lExact>], ;
[<lASCII>] ) --> nElement
Arguments
<aArray>
This is the array to iterate.
<xbSearch>
<xbSearch> is either the value to search in <aArray> or a code block containing the search condition. The code block receives a single parameter which is the value stored in the current array element. The code block must return a logical value. When the return value is .T. (true), the function stops searching and returns the position of the corresponding array element.
<nStart>
This is a numeric expression indicating the first element in the array to begin the search with. It defaults to 1, the first element of <aArray>.
<nCount>
A numeric expression specifying the number of elements to search. It defaults to 1+Len(<aArray>)-<nStart>.
<lExact>
This parameter influences the comparison for searching character strings in <aArray>. If .T. (true) is passed, an exact string comparison is performed. When omitted or if .F. (false) is passed, string comparison follows the SET EXACT rules.
<lASCII>
This parameter is only relevant for arrays that contain single characters in their elements. A single character is treated like a numeric ASCII value when <lASCII> is .T. (true). The default is.F. (false). Return
AScan() returns the numeric ordinal position of the array element that contains the searched value. When no match is found, the return value is 0.
Description
The array function AScan() traverses the array <aArray> for the value specified with <xbSearch>. If <xbSearch> is not a code block, AScan() compares the values of each array element for being equal with the searched value. If this comparison yields .T. (true), the function stops searching and returns the numeric position of the array element containing the searched value.
If a code block is passed for <xbSearch>, it is evaluated and receives as parameter the value of the current array element. The code block must contain a comparison rule that yields a logical value. AScan() considers the value as being found when the codeblock returns .T. (true). Otherwise the function proceeds with the next array element.
Note: use function RAscan() to search an array beginning with the last element.
Vejam a 3ª comparação no exemplo abaixo:SET EXACT
Determines the mode for character string comparison.
Syntax
SET EXACT on | OFF | ( <lOnOff> )
Arguments
on | OFF | ( <lOnOff> )
This option toggles if an exact comparison is performed with character strings. or not. The default is OFF or .F. (false), i.e. characters are compared up the length of the shorter string. To change the setting use ON or .T. (true) as parameter. The parameter can also be specified as a logical expression enclosed in parentheses. Description
The SET EXACT command determines the mode for character string comparison with the comparison operators (=, >, <, =>, =<). The following rules are applied for comparison with SET EXACT set to ON:
1) When the right operand is a null string, the result is always .T. (true)
2) When the right operand is longer than the left operand, the result is always .F. (false)
3) When the right operand contains less or the same number of characters as the left operand, the result is only true if the left operand begins with the exact same characters as the right operand.
With SET EXACT set to OFF, the same rules apply except that trailing spaces are ignored in the comparison.
Notes: The exact equal operator == always performs an exact comparison irrespectively of the SET EXACT setting.
Search commands and functions such as SEEK, DbSeek() or SET RELATEION are not affected
Código: Selecionar todos
PROCEDURE Main
SET EXACT OFF
? "AB" = "ABCD" // result: .F.
? "ABCD" = "AB" // result: .T.
? "AB" = "" // result: .T.
? "" = "AB" // result: .F.
? "AB" = "AB " // result: .F.
SET EXACT ON
? "AB" = "ABCD" // result: .F.
? "ABCD" = "AB" // result: .F.
? "AB" = "" // result: .F.
? "" = "AB" // result: .F.
? "AB" = "AB " // result: .T.
? "AB" == "AB " // result: .F.
RETURN
ExampleRAscan()
Searches a value in an array beginning with the last element.
Syntax
RAscan( <aArray> , ;
<xbSearch>, ;
[<nStart>] , ;
[<nCount>] , ;
[<lExact>] , ;
[<lASCII>] ) --> nElement
Arguments
<aArray>
This is the array to iterate.
<xbSearch>
<xbSearch> is either the value to search in <aArray> or a code block containing the search condition. The code block receives a single parameter which is the value stored in the current array element. The code block must return a logical value. When the return value is .T. (true), the function stops searching and returns the position of the corresponding array element.
<nStart>
This is a numeric expression indicating the first element in the array to begin the search with. It defaults to Len(<aArray>), the last element of the array.
<nCount>
A numeric expression specifying the number of elements to search. It defaults to 1+Len(<aArray>)-<nStart>.
<lExact>
This parameter influences the comparison for searching character strings in <aArray>. If .T. (true) is passed, an exact string comparison is performed. When omitted or if .F. (false) is passed, string comparison follows the SET EXACT rules.
<lASCII>
This parameter is only relevant for arrays that contain single characters in their elements. A single character is treated like a numeric ASCII value when <lASCII> is .T. (true). The default is.F. (false). Return
RAScan() returns the numeric ordinal position of the array element that contains the searched value. When no match is found, the return value is 0.
Description
The array function RAscan() traverses the array <aArray> for the value specified with <xbSearch>. If <xbSearch> is not a code block, RAscan() compares the values of each array element for being equal with the searched value. If this comparison yields .T. (true), the function stops searching and returns the numeric position of the array element containing the searched value.
If a code block is passed for <xbSearch>, it is evaluated and receives as parameter the value of the current array element. The code block must contain a comparison rule that yields a logical value. AScan() considers the value as being found when the codeblock returns .T. (true). Otherwise the function proceeds with the next array element.
Note: use function AScan() to search an array beginning with the first element.
Info
See also: AEval(), Asc(), Ascan(), ASort(), ATail(), Eval(), IN, SET EXACT
Category: Array functions , xHarbour extensions
Source: vm\arrayshb.c
LIB: xhb.lib
DLL: xhbdll.dll
Código: Selecionar todos
// The example demonstrates the difference between AScan() and RAscan().
PROCEDURE Main()
LOCAL aArray := { "A", 1, Date(), 1, .F. }
? Ascan( aArray, 1 ) // result: 2
? RAScan( aArray, 1 ) // result: 4
RETURN
Código: Selecionar todos
Func Main()
Loca := aMatriz := {'a','b','c'}, lSetExac := SET(_SET_EXACT, .t. )
? ascan(aMatriz,'b') // Retorna 2
? ascan(aMatriz,'') // Retorna 0
SET(_SET_EXACT, lSetExac) // seta SET EXACT anterior
Retu