table process selected rows fast

Prev Next

Function Names

table process selected rows fast

Description

In contrast to table process selected rows(), you can achieve significant performance improvment if the targeted table is sorted and and the Boolean expression tests at least on the sorting value. It becomes useful when calling this function from a loop processing the same or different table, including calling out from code inside table process() / table process selected rows().

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled
This function provides a table context for partial table specifications with table name and row number for selected function parameters

Parameter count

5

Parameters

No.TypeDescription
1.
input
string Name of existing table

2.
code
expression
:string
Expression to select rows

Specify the conditions or rules to select the rows. See expressions to select rows.
The table context for partial table specifications is available for referencing just the columns easily. Example: [Year]>=2022.
Attention: Comparison operators = and <> (instead of == and !=) may hijack the next function parameters for its own use as additional comparison operands, e.g. a[]=1,3,5. As long this comparison is not made in the last function parameter, then put parentheses around them, e.g. ... , (a[]=1,3,5), ....

3.
io
numeral Starting row number

Specify the starting row number to checking and processing. The range is between 1 and total number of rows minus 1. 0 will be corrected to 1.
It must be variable which will be updated with the row number to use next. After function call is completed, the row number may have changed to a bigger value. Attenetion: This function does not support negative indexing (e.g. -1 = last row). Processing will be done from top to bottom.

4.
input
string Processing option

Use one of the following values: continue

  • Begin checking at the row as specified in the 3rd function parameter.
  • If expression does not yet match, then continue checking the following rows (until end of table has been reached).
  • If expression matches, then execute statements and increment row number (to be returned). Then continue checking the expression in the next row.
  • If expression no longer matches, then stop checking.
  • 3rd function parameter will either return next row after last row processed, or, if there was no match, the row number of last row + 1 (= table length value).

stop

  • Begin checking at the row as specified in the 3rd function parameter.
  • If expression does not yet match, then stop immediately and leave the row number provided in the 3rd function parameter unchanged.
  • If expression matches, then execute statements and increment row number (to be returned). Then continue checking the expression in the next row.
  • If expression no longer matches, then stop checking.
  • 3rd function parameter will either return next row after last row processed.

repeat works similarly as continue, but following differences apply:

  • If a match has been found, then the 1st matching row number is returned in the 3rd function parameter.
  • If no match has been found, then the supplied row number will stay unchanged.

repeat remaining works similarly as repeat, but following differences apply:

  • It will check all matches till the end of the table.

repeat revolving works similarly as repeat, but following differences apply:

  • If no matches are found from specified row number till end of table, then it will start at row 1 to check the rows located above.

Hint: See also illustration in the next section, Additional info on fast processing of rows.

5.
code
statements
:string
Statements (applicable where rows are selected)

table process: The statements provided will be executed for every row.
table process selected rows: The statements provided will be executed for every row where the condition calculated before returned true.

The table context for partial table specifications is available for referencing just the columns easily. Example: [Full Name]=[First Name]+' '+[Last Name];.
Hint: If you encounter an error message at the very end of the program after you worked on a statement, you may proabably have forgotton to add the closing parentheses to close the function call.

Return value

TypeDescription
numeral Number of rows processed

Examples

       table initialize ( t, { { Name, Game Score, Done},
         { Andy, 21 }, { Andy, 12 }, { Andy, 20 }, { Max,  50 }, { Max,  31 },
         { Tina, 32 }, { Andy, 44 }, { Andy, 16 }, { Max,  25 }, { Tina, 19 } } );


       // table sort rows ( t, Name );
       echo("Original table, sorted by name: ");
       table list( t );

       echo("Example 1: Demonstrate processing option 'continue':");
       table copy table ( t, t1 );
       row[] = 1; counter[] = 1;
       do
       {
               c[] = table process selected rows fast( t1, [Name]==Andy, row[], continue, [Done] = counter[]++  );
               echo("Row number returned: ", row[], ",  Items processed: ", c[] );
       }
       while (c[] > 0);
       table list( t1 );

       echo("Example 2: Demonstrate processing option 'stop':");
       table copy table ( t, t1 );
       row[] = 1; counter[] = 1;
       do
       {
               c[] = table process selected rows fast( t1, [Name]==Andy, row[], stop, [Done] = counter[]++  );
               echo("Row number returned: ", row[], ",  Items processed: ", c[] );
       }
       while (c[] > 0);
       table list( t1 );

       echo("Example 3: Demonstrate processing option 'repeat':");
       table copy table ( t, t1 );
       row[] = 1; counter[] = 1;
       c[] = table process selected rows fast( t1, [Name]==Max, row[], repeat, [Done] = counter[]++  ); // Stops
       echo("Row number returned: ", row[], ",  Items processed: ", c[] );
       row[] = 6;
       c[] = table process selected rows fast( t1, [Name]==Max, row[], repeat, [Done] = counter[]++  ); // Processing
       echo("Row number returned: ", row[], ",  Items processed: ", c[] );
       row[]++; // Next row
       c[] = table process selected rows fast( t1, [Name]==Max, row[], repeat, [Done] = counter[]++  ); // Processing
       echo("Row number returned: ", row[], ",  Items processed: ", c[] );
       table list( t1 );

       echo("Example 4: Demonstrate processing option 'repeat remaining':");
       table copy table ( t, t1 );
       row[] = 3; counter[] = 1;
       c[] = table process selected rows fast( t1, [Name]==Andy, row[], repeat remaining, [Done] = counter[]++  ); // Stops
       echo("Row number returned: ", row[], ",  Items processed: ", c[] );
       table list( t1 );

       echo("Example 5: Demonstrate processing option 'repeat revolving':");
       table copy table ( t, t1 );
       row[] = 10; counter[] = 1;  // Will find nothing below row 10, so continue from beginning
       c[] = table process selected rows fast( t1, [Name]==Andy, row[], repeat revolving, [Done] = counter[]++  ); // Stops
       echo("Row number returned: ", row[], ",  Items processed: ", c[] );
       table list( t1 );

Output

Original table, sorted by name:
    0 : Name | Game Score | Done
    1 : Andy | 21         |     
    2 : Andy | 12         |     
    3 : Andy | 20         |     
    4 : Max  | 50         |     
    5 : Max  | 31         |     
    6 : Tina | 32         |     
    7 : Andy | 44         |     
    8 : Andy | 16         |     
    9 : Max  | 25         |     
   10 : Tina | 19         |     

Example 1: Demonstrate processing option 'continue':
Row number returned: 4,  Items processed: 3
Row number returned: 9,  Items processed: 2
Row number returned: 11,  Items processed: 0
    0 : Name | Game Score | Done
    1 : Andy | 21         | 1   
    2 : Andy | 12         | 2   
    3 : Andy | 20         | 3   
    4 : Max  | 50         |     
    5 : Max  | 31         |     
    6 : Tina | 32         |     
    7 : Andy | 44         | 4   
    8 : Andy | 16         | 5   
    9 : Max  | 25         |     
   10 : Tina | 19         |     

Example 2: Demonstrate processing option 'stop':
Row number returned: 4,  Items processed: 3
Row number returned: 4,  Items processed: 0
    0 : Name | Game Score | Done
    1 : Andy | 21         | 1   
    2 : Andy | 12         | 2   
    3 : Andy | 20         | 3   
    4 : Max  | 50         |     
    5 : Max  | 31         |     
    6 : Tina | 32         |     
    7 : Andy | 44         |     
    8 : Andy | 16         |     
    9 : Max  | 25         |     
   10 : Tina | 19         |     

Example 3: Demonstrate processing option 'repeat':
Row number returned: 4,  Items processed: 2
Row number returned: 9,  Items processed: 1
Row number returned: 10,  Items processed: 0
    0 : Name | Game Score | Done
    1 : Andy | 21         |     
    2 : Andy | 12         |     
    3 : Andy | 20         |     
    4 : Max  | 50         | 1   
    5 : Max  | 31         | 2   
    6 : Tina | 32         |     
    7 : Andy | 44         |     
    8 : Andy | 16         |     
    9 : Max  | 25         | 3   
   10 : Tina | 19         |     

Example 4: Demonstrate processing option 'repeat remaining':
Row number returned: 3,  Items processed: 3
    0 : Name | Game Score | Done
    1 : Andy | 21         |     
    2 : Andy | 12         |     
    3 : Andy | 20         | 1   
    4 : Max  | 50         |     
    5 : Max  | 31         |     
    6 : Tina | 32         |     
    7 : Andy | 44         | 2   
    8 : Andy | 16         | 3   
    9 : Max  | 25         |     
   10 : Tina | 19         |     

Example 5: Demonstrate processing option 'repeat revolving':
Row number returned: 1,  Items processed: 3
    0 : Name | Game Score | Done
    1 : Andy | 21         | 1   
    2 : Andy | 12         | 2   
    3 : Andy | 20         | 3   
    4 : Max  | 50         |     
    5 : Max  | 31         |     
    6 : Tina | 32         |     
    7 : Andy | 44         |     
    8 : Andy | 16         |     
    9 : Max  | 25         |     
   10 : Tina | 19         |     

Try it yourself: Open LIB_Function_table_process_selected_rows_fast.b4p in B4P_Examples.zip. Decompress before use.

See also

table process selected rows