Page 1 of 1

ASCAN question

PostPosted: Wed Apr 26, 2017 11:09 pm
by TimStone
I have an array (aWork ) , and each element is an array with 4 elements:
The data arrays are:
[1] is a character field of 5 digits
[2] is a counter
[3] is an accumulator ( totals )
[4] is a description

I want to use aScan on aWork on the first element, and when I find the matching array, I will increment [2] and [3].

I can I use ASCAN( ) to search an element in an array of arrays ?

Re: ASCAN question

PostPosted: Thu Apr 27, 2017 12:59 am
by George
Tim,
From xharbour Language Reference Guide:
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.
Code: Select all  Expand view
The example demonstrates simple and complex searching in arrays.
   PROCEDURE Main()
      LOCAL aArray := { "A", 1, Date(), NIL, .F. }

      // one dimensional array
      ? AScan( aArray, 1 )             // result: 2
      ? AScan( aArray, .F. )           // result: 5

      ? AScan( aArray, {|x| Valtype(x)=="D" } )
                                       // result: 3

      // two dimensional array
      aArray := Directory( "*.prg" )

      ? AScan( aArray, {|a| Upper(a[1]) == "TEST.PRG" } )
                                       // result: 28
   RETURN

Maybe by using the codeblock you can find a solution.

Re: ASCAN question

PostPosted: Thu Apr 27, 2017 3:11 am
by nageswaragunupudi
Code: Select all  Expand view
if ( nAt := AScan( aWork, { |a| a[ 1 ] == cSeek } ) ) > 0
   aWork[ nAt, 2 ] += 1
   aWork[ nAt, 3 ] += nAmount
endif