14.01.2020 Views

ABAP_to_the_Future

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Internal Tables 2.6

In Section 2.2.2, you learned that from 7.4 onwards you no longer need to do a

DATA declaration for elementary data types. It is exactly the same for the work

areas, which are of course structures. If you are loopin g into a work area called

LS_MONSTERS and the work area structure does not match the type of the table LT_

MONSTERS, you would get a syntax error.

Therefore, the compiler knows the type it wants—so why do you have to tell it

that type in an explicit da ta declaration? The answer is that now you don’t, and

this change will remove all those extra data declaration lines you had to insert

when everyone moved away from header lines (if they ever did) and had to

explicitly declare work areas instead.

In the same way that you no longer need DATA declarations for table work areas,

you also no longer need FIELD-SYMBOL declarations for the (common) situations

in which you want to change the data in the work area while looping through an

internal table. In release 7.4, if you want to use field symbols for the work area,

then the syntax is as shown in Listing 2.47.

READ TABLE lt_monsters WITH KEY monster_number = ld_monster_

number ASSIGNING FIELD-SYMBOL(<ls_monsters>).

LOOP AT lt_monsters ASSIGNING FIELD-SYMBOL(<ls_monsters>).

Listing 2.47 Field Symbols for Work Area

One vitally important point to note is th at if you declare a work area and then

loop through an internal area, after the lo op is over the work area will be filled

with the value of the last row of the in ternal table that was processed. However,

if you loop through an internal table into a work area created via the DATA statement,

then you might wonder if the work area ceases to exist after you leave the

loop.

From an academic point of view, not being able to access the work area outside of

the loop is a Good Thing, because the te xtbooks all say that variables should not

exist outside their scope (the loop, in this case). And, indeed, at SAP this behavior

is very much desired. Unfortunately, the fa ct is that the variable does exist after

you leave the loop. SAP has got around thisproblem by publishing guidelines saying

that if you access such a variable outside of its scope, then you are very

naughty, and the staff at SAP will say “tut, tut” if they find out. (Personally, I am

not convinced that this dire threat will have much effect, but I thought I would let

you know.)

113

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

Saved successfully!

Ooh no, something went wrong!