ABAP News for 7.40, SP08 – More for Internal Tables
Besides the already mentioned REDUCE operator, the conditional FOR and LINES OF inside VALUE & Co. and the grouping of internal tables, there are some other ABAP enhancements for internal tables in 7.40, SP08:
The new FILTER operator enables two kinds of filtering an internal table.
FILTER with single values
In this variant, you simply extrract the lines from an internal table into a tabular result, that fulfill a simple value condition.
FILTER #( spfli_tab USING KEY carr_city
WHERE carrid = CONV #( to_upper( carrid ) ) AND
cityfrom = CONV #( to_upper( cityfrom ) ) ).
As a prerequisite, the filtered table must have a sorted or a hash key (primary or secondary), that is evaluated behind WHERE. You can also extract the lines that do not fulfill the WHERE condition by using an EXCEPT addition. Of course, you can achieve the same result by using a FOR inside a VALUE ror a REDUCE expression but FILTER allows you to write it shorter and it should be faster.
FILTER with filter table
In this variant, you compare the lines of one table with the contents of another table, the filter table, and you extract those lines, where at least one match is found (say hello to FOR ALL ENTRIES).
TYPES: BEGIN OF filter,
cityfrom TYPE spfli-cityfrom,
cityto TYPE spfli-cityto,
END OF filter,
filter_tab TYPE HASHED TABLE OF filter
WITH UNIQUE KEY cityfrom cityto.
DATA(filter_tab) = …
FILTER #( spfli_tab IN filter_tab
WHERE cityfrom = cityfrom AND cityto = cityto ).
Here, the filter table – that can be specified also as a functional method call – must have a sorted or a hashed key (primary or secondary) that is evaluated.
Standard Value for Table Expressions
Table expressions itab[ …] cannot support sy-subrc. Up to now, an exception was raised anytime if a table line specified in the square brackets could not be found. Not everybody liked this behavior.
As a workaround, you can place a table expression inside a VALUE or REF expression, that contains a OPTIONAL or DEFAULT addition. If a line is not found, the OPTIONAL addition returns an initiial line while the DEFAULT addition returns a given value, that can be specified as an expression, especially another table expression.
BEGIN OF line,
id TYPE i,
value TYPE string,
END OF line,
itab TYPE SORTED TABLE OF line WITH UNIQUE KEY id.
DATA(def) = VALUE line( id = 0 value = `not found` ).
DATA(result) = VALUE #( itab[ id = … ] DEFAULT def ).