Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

URGENT!!!!! Field Symbols.

Former Member
0 Kudos
783

Hi Experts ,

I have a structure say , gs_str having fields:

P, A01 ,B01 ,C01 ,A02, B02, C02, ..............A20, B20, C20.

Even I have an Internal table say , gt_data having fields P, A ,B , C.

Here is the Sample Code :

Loop at gt_data into gs_data.

-


Here i have to find which value of gs_str-Ann will have same value as gs_data-A.

if gs_data-A = gs_str-Ann.

-


then do the following arithmetic operation for corresponding gs_str-Bnn ,

gs_data-C = gs_str-Bnn + 10.

Modify gt_data with gs_data.

endif.

endloop.

I believe it can be done using field symbols.....Pls suggest some way or provide a code extract.

Thanks.

1 ACCEPTED SOLUTION

mnicolai_77
Active Participant
0 Kudos
180

hi,

try this way

- First example

>data: i(2) type n.

>data: str(3) type c.

>field-symbols <fs> type any.

>loop at gt_data into gs_data.

> clear i.

> do.

> add 1 to i.

> concatenate 'A' i into str.

> assign component (str) of structure gs_str to <fs>.

> if sy-subrc <> 0. exit. endif.

> if gs_data-a = <fs>.

> unassign <fs>.

> clear str.

> concatenate 'B' i into str.

> assign component (str) of structure gs_str to <fs>.

> gs_data-c = <fs> + 10.

> unassign <fs>.

> clear str.

> exit.

> endif.

> enddo.

> modify gt_data from gs_data.

>endloop.

- second example

>data: i(2) type n.

>data: str(3) type c.

>field-symbols <fs> type any,

> <fs_data> like line of gt_data.

>loop at gt_data assigning <fs_data>

> clear i.

> do.

> add 1 to i.

> concatenate 'A' i into str.

> assign component (str) of structure <fs_data> to <fs>.

> if sy-subrc <> 0. exit. endif.

> if <fs_data>-a = <fs>.

> unassign <fs>.

> clear str.

> concatenate 'B' i into str.

> assign component (str) of structure <fs_data> to <fs>.

> <fs_data>-c = <fs> + 10.

> unassign <fs>.

> clear str.

> exit.

> endif.

> enddo.

>endloop.

hope that is usefull.

regards

Marco

3 REPLIES 3

mnicolai_77
Active Participant
0 Kudos
181

hi,

try this way

- First example

>data: i(2) type n.

>data: str(3) type c.

>field-symbols <fs> type any.

>loop at gt_data into gs_data.

> clear i.

> do.

> add 1 to i.

> concatenate 'A' i into str.

> assign component (str) of structure gs_str to <fs>.

> if sy-subrc <> 0. exit. endif.

> if gs_data-a = <fs>.

> unassign <fs>.

> clear str.

> concatenate 'B' i into str.

> assign component (str) of structure gs_str to <fs>.

> gs_data-c = <fs> + 10.

> unassign <fs>.

> clear str.

> exit.

> endif.

> enddo.

> modify gt_data from gs_data.

>endloop.

- second example

>data: i(2) type n.

>data: str(3) type c.

>field-symbols <fs> type any,

> <fs_data> like line of gt_data.

>loop at gt_data assigning <fs_data>

> clear i.

> do.

> add 1 to i.

> concatenate 'A' i into str.

> assign component (str) of structure <fs_data> to <fs>.

> if sy-subrc <> 0. exit. endif.

> if <fs_data>-a = <fs>.

> unassign <fs>.

> clear str.

> concatenate 'B' i into str.

> assign component (str) of structure <fs_data> to <fs>.

> <fs_data>-c = <fs> + 10.

> unassign <fs>.

> clear str.

> exit.

> endif.

> enddo.

>endloop.

hope that is usefull.

regards

Marco

Former Member
0 Kudos
180

Hi Shital,

With field symbols you don't have to modify the internal table as the line itself is getting modified (and not any work area).

See below:


field-symbols: <gs_data> like line of gt_data. "TYPE reference can be used using type of gt_data

Loop at gt_data assigning <gs_data>.

"--Here i have to find which value of gs_str-Ann will have same value as gs_data-A.

 if <gs_data>-A EQ gs_str-Ann.

"--then do the following arithmetic operation for corresponding gs_str-Bnn ,

  <gs_data>-C = gs_str-Bnn + 10.

"Modify gt_data with gs_data. ------------Not required anymore

 endif.
endloop.

You can define a field symbol for gs_str and use it in place of gs_str (not done above).

Hope this helps.

Thanks

Sanjeev

Former Member
0 Kudos
180

Hi Shital,

check this.

Syntax

FIELD-SYMBOLS <fs> { typing | STRUCTURE struc DEFAULT dobj }.

Extras:

1. ... typing

2. ... STRUCTURE struc DEFAULT dobj

Effect

The FIELD-SYMBOLS statement declares a field symbol <fs>. The naming conventions apply to the name fs. The angle brackets of the field symbols indicate the difference to data objects and are obligatory. You can declare field symbols in any procedure and in the global declaration section of an ABAP program, but not in the declaration section of a class or an interface. You can use a field symbol in any operand position in which it is visible and which match the typing defined using typing.

After its declaration, a field symbol is initial - that is, it does not reference a memory area. You have to assign a memory area to it (normally using the ASSIGN statement) before you can use it as an operand. Otherwise an exception will be triggered.

Addition 1

... typing

Effect

You can use the addition typing to type the field symbol. The syntax of typing is described under Syntax of Typing. The typing specifies which memory areas can be assigned to the field symbol (see Checking the Typing) and in which operand positions it can be used.

Note

You can omit the addition typing outside of methods. In this case, the field symbol has the complete generic type any and is implicitly assigned the predefined constant space during the declaration.

Addition 2

... STRUCTURE struc DEFAULT dobj

Effect

If you specify the addition STRUCTURE instead of typing for a field symbol, and struc is a local program structure (a data object, not a data type) or a flat structure from the ABAP Dictionary, this structure is cast for the field symbol <fs>. You have to specify a data object dobj that is initially assigned to the field symbol.

The field symbol copies the technical attributes of structure struc as if it were completely typed. When you assign a data object using the addition DEFAULT, or later using ASSIGN, its complete data type is not checked in non- Unicode programs. Instead, the system merely checks whether it has at least the length of the structure and its alignment.

In Unicode programs, we differentiate between structured and elementary data objects. For a structured data object dobj, its Unicode fragment view has to match the one of struc. In the case of an elementary data object, the object must be character-type and flat, and struc must be purely character-type. The same applies to assignments of data objects to field symbols typed using STRUCTURE when using the ASSIGN statement.

Note

Field symbols declared using the addition STRUCTURE are a mixture of typed field symbols and a utility for casting structured data types. You should use the additions TYPE or LIKE for the FIELD-SYMBOLS statement to type field symbols, while the addition CASTING of the ASSIGN statement is used for casting.

Example

The first example shows the obsolete usage of the addition STRUCTURE.

DATA wa1 TYPE c LENGTH 512.

FIELD-SYMBOLS <scarr1> STRUCTURE scarr DEFAULT wa1.

<scarr1>-carrid = '...'.

The second example shows the replacement of STRUCTURE with the additions TYPE and CASTING.

DATA wa2 TYPE c LENGTH 512.

FIELD-SYMBOLS <scarr2> TYPE scarr.

ASSIGN wa2 TO <scarr2> CASTING.

<scarr2>-carrid = '...'.

Also, this will give u clear idea about field symbols.

Field Symbols

Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. A field symbol cam point to any data object. The data object to which a field symbol points is assigned to it after it has been declared in the program.

Whenever you address a field symbol in a program, you are addressing the field that is assigned to the field symbol. After successful assignment, there is no difference in ABAP whether you reference the field symbol or the field itself. You must assign a field to each field symbol before you can address the latter in programs.

Field symbols are similar to dereferenced pointers in C (that is, pointers to which the content operator * is applied). However, the only real equivalent of pointers in ABAP, that is, variables that contain a memory address (reference) and that can be used without the contents operator, are reference variables in ABAP Objects.

All operations programmed with field symbols are applied to the field assigned to it. For example, a MOVE statement between two field symbols moves the contents of the field assigned to the first field symbol to the field assigned to the second field symbol. The field symbols themselves point to the same fields after the MOVE statement as they did before.

You can create field symbols either without or with type specifications. If you do not specify a type, the field symbol inherits all of the technical attributes of the field assigned to it. If you do specify a type, the system checks the compatibility of the field symbol and the field you are assigning to it during the ASSIGN statement.

Field symbols provide greater flexibility when you address data objects:

If you want to process sections of fields, you can specify the offset and length of the field dynamically.

You can assign one field symbol to another, which allows you to address parts of fields.

Assignments to field symbols may extend beyond field boundaries. This allows you to address regular sequences of fields in memory efficiently.

You can also force a field symbol to take different technical attributes from those of the field assigned to it.

The flexibility of field symbols provides elegant solutions to certain problems. On the other hand, it does mean that errors can easily occur. Since fields are not assigned to field symbols until runtime, the effectiveness of syntax and security checks is very limited for operations involving field symbols. This can lead to runtime errors or incorrect data assignments.

While runtime errors indicate an obvious problem, incorrect data assignments are dangerous because they can be very difficult to detect. For this reason, you should only use field symbols if you cannot achieve the same result using other ABAP statements.

For example, you may want to process part of a string where the offset and length depend on the contents of the field. You could use field symbols in this case. However, since the MOVE statement also supports variable offset and length specifications, you should use it instead. The MOVE statement (with your own auxiliary variables if required) is much safer than using field symbols, since it cannot address memory beyond the boundary of a field. However, field symbols may improve performance in some cases.

check the below links u will get the answers for your questions

http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3860358411d1829f0000e829fbfe/content.htm

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/field_sy.htm

http://searchsap.techtarget.com/tip/1,289483,sid21_gci920484,00.html

Syntax Diagram

FIELD-SYMBOLS

Basic form

FIELD-SYMBOLS <fs>.

Extras:

1. ... TYPE type

2. ... TYPE REF TO cif

3. ... TYPE REF TO DATA

4. ... TYPE LINE OF type

5. ... LIKE s

6. ... LIKE LINE OF s

7. ... TYPE tabkind

8. ... STRUCTURE s DEFAULT wa

The syntax check performed in an ABAP Objects context is stricter than in other ABAP areas. See Cannot Use Untyped Field Symbols ad Cannot Use Field Symbols as Components of Classes.

Effect

This statement declares a symbolic field called <fs>. At runtime, you can assign a concrete field to the field symbol using ASSIGN. All operations performed with the field symbol then directly affect the field assigned to it.

You can only use one of the additions.

Example

Output aircraft type from the table SFLIGHT using a field symbol:

FIELD-SYMBOLS <PT> TYPE ANY.

DATA SFLIGHT_WA TYPE SFLIGHT.

...

ASSIGN SFLIGHT_WA-PLANETYPE TO <PT>.

WRITE <PT>.

Addition 1

... TYPE type

Addition 2

... TYPE REF TO cif

Addition 3

... TYPE REF TO DATA

Addition 4

... TYPE LINE OF type

Addition 5

... LIKE s

Addition 6

... LIKE LINE OF s

Addition 7

... TYPE tabkind

Effect

You can define the type of the field symbol using additions 2 to 7 (just as you can for FORM parameters (compare Defining the Type of Subroutine Parameters). When you use the ASSIGN statement, the system carries out the same type checks as for USING parameters of FORMs.

This addition is not allowed in an ABAP Objects context. See Cannot Use Obsolete Casting for FIELD SYMBOLS.

In some cases, the syntax rules that apply to Unicode programs are different than those for non-Unicode programs. See Defining Types Using STRUCTURE.

Effect

Assigns any (internal) field string or structure to the field symbol from the ABAP Dictionary (s). All fields of the structure can be addressed by name: <fs>-fieldname. The structured field symbol points initially to the work area wa specified after DEFAULT.

The work area wa must be at least as long as the structure s. If s contains fields of the type I or F, wa should have the structure s or at least begin in that way, since otherwise alignment problems may occur.

Example

Address components of the flight bookings table SBOOK using a field symbol:

DATA SBOOK_WA LIKE SBOOK.

FIELD-SYMBOLS <SB> STRUCTURE SBOOK

DEFAULT SBOOK_WA.

...

WRITE: <SB>-BOOKID, <SB>-FLDATE.

Related

ASSIGN, DATA

Additional help

Declaring Field Symbols

kindly reward if found helpful.

cheers,

Hema.