ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Meshes → Meshes - Using Mesh Paths → FOR ... IN mesh_path
FOR Expressions for Mesh Paths
This example demonstrates table comprehensions and table reductions for mesh paths.
Other versions:
7.31 | 7.40 | 7.54
Source Code
input( ).
TRY.
out->begin_section(
'Initial Association SCARR\to_spfli' ).
DATA(spfli_tab) = VALUE t_spfli(
FOR spfli_wa IN
flights-scarr\to_spfli[ flights-scarr[ carrname = name ]
WHERE countryto = country ]
( spfli_wa ) ).
out->write( spfli_tab ).
DATA(distance) = REDUCE spfli-distance(
INIT d TYPE spfli-distance
FOR spfli_wa IN
flights-scarr\to_spfli[ flights-scarr[ carrname = name ]
WHERE countryto = country ]
NEXT d = d + SWITCH spfli-distance( spfli_wa-distid
WHEN 'KM'
THEN spfli_wa-distance * '1.60934'
WHEN 'MI'
THEN spfli_wa-distance
ELSE 0 ) ).
out->write( |Sum of distances in miles: { distance }| ).
out->next_section(
'Chained Association SCARR\to_spfli\to_sairport' ).
DATA(sairport_tab) = VALUE t_sairport(
FOR sairport_wa IN
flights-scarr\to_spfli[ flights-scarr[ carrname = name ]
WHERE countryto = country
]\to_sairport[
USING KEY primary_key ]
( sairport_wa ) ).
out->write( sairport_tab ).
out->next_section(
'Chained Association' &&
' SCARR\to_spfli\to_sflight\^to_sflight~spfli' ).
spfli_tab = VALUE t_spfli(
FOR spfli_wa IN
flights-scarr\to_spfli[ flights-scarr[ carrname = name ]
WHERE countryto = country
]\to_sflight[
WHERE planetype = plane1 OR
planetype = plane2
]\^to_sflight~spfli[ ]
( spfli_wa ) ).
out->write( spfli_tab ).
CATCH cx_sy_itab_line_not_found.
out->write( 'Exception!' ).
ENDTRY.
out->display( ).
Description
This example uses the same mesh paths as the example
for LOOP AT and produces the same results sets. Unlike
LOOP AT, the rows in the results set are produced in a results table using
table comprehensions and not as individual rows.
For the first mesh path, a table
reduction is also performed using the reduction operator
REDUCE. This totals the distances, with the conversion from kilometers
to miles done using a SWITCH expression.