14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2

New Language Features in ABAP 7.4

2.6.6 New Functions for Common Internal Table Tasks

When working in ABAP code, there are cases in which you want to know in

exactly what line of an internal table the data you are interested in lives. To find

this information out in pre-7.4 ABAP, you would have written code that declared

a helper variable to store the row number of the target record, read the table for

no other purpose than to find that row number, and then transferred the system

variable that contained the result of the table read into your helper variable. An

example of this is shown in Listing 2.57.

DATA: ld_tabix TYPE sy-tabix.

READ TABLE lt_monsters WITH KEY monster_number = ld_monster_

number TRANSPORTING NO FIELDS.

IF sy-subrc = 0.

ld_tabix = sy-tabix.

ENDIF.

Listing 2.57 Reading an Internal Table to Get the Row Number

You see the logic in Listing 2.57 a lot when processing nested loops—you follow

such code with a LOOP AT itab STARTING AT ld_tabix —but also in a myriad of

other use cases. In any event, in release 7.4 this can be simplified by using the

built-in function LINE_INDEX, which does the exact same task but without the

need for looking at the values of SY-SUBRC and SY-TABIX (STARTING AT LINE_

INDEX, etc.). This is shown in Listing 2.58.

DATA(ld_tabix) =

LINE_INDEX( lt_monsters[ monster_number = ld_monster_number] ).

LOOP AT lt_monsters STRTING AT ld_tabix.

Listing 2.58 LINE_INDEX

Throughout this chapter, one aim has been to get rid of helper variables. Because

LINE_INDEX is a so-called built-in function, it can be used at operand positions,

thus negating the need for the helper variable LD_TABIX. So when looping at an

internal table from the row formerly stored in LD_TABIX instead, use the code

shown in Listing 2.59.

LOOP AT lt_monsters STARTING AT LINE_INDEX(etc)

Listing 2.59 Built-In Function LINE_INDEX at an Operand Position

This new built-in function also has a friend: LINE_EXISTS. Say that you want to see

if an internal table already has an entr y for the monster at hand. If it does, then

120

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!