Monday, December 15, 2008

Data references

The following is the data reference equivalent to the example code using an object. It's shorter but less clear (IMHO).


TYPES:
BEGIN OF t_place,
place TYPE char30,
END OF t_place,
t_place_tab TYPE TABLE OF t_place,
BEGIN OF t_city,
city_name TYPE char20,
places_ref TYPE REF TO data,
END OF t_city.

FIELD-SYMBOLS:
<ref> TYPE t_place_tab.

DATA:
gt_city TYPE HASHED TABLE OF t_city WITH UNIQUE KEY city_name,
gs_city TYPE t_city,
gs_place TYPE t_place.

START-OF-SELECTION.
gs_city-city_name = 'Porto'.
CREATE DATA gs_city-places_ref TYPE t_place_tab.
ASSIGN gs_city-places_ref->* TO <ref>.
gs_place-place = 'Torre dos Clerigos'.
APPEND gs_place TO <ref>.
gs_place-place = 'Casa da Musica'.
APPEND gs_place TO <ref>.
gs_place-place = 'Serralves'.
APPEND gs_place TO <ref>.

INSERT gs_city INTO TABLE gt_city.

gs_city-city_name = 'New York'.
CREATE DATA gs_city-places_ref TYPE t_place_tab.
ASSIGN gs_city-places_ref->* TO <ref>.
gs_place-place = 'Empire State Building'.
APPEND gs_place TO <ref>.
gs_place-place = 'Central Park'.
APPEND gs_place TO <ref>.

INSERT gs_city INTO TABLE gt_city.

READ TABLE gt_city INTO gs_city WITH KEY city_name = 'Porto'.
IF sy-subrc = 0.
ASSIGN gs_city-places_ref->* TO <ref>.
SORT <ref> BY place.
LOOP AT <ref> INTO gs_place.
WRITE: / gs_city-city_name, gs_place-place.
ENDLOOP.
ENDIF.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home