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: 

simple way in ABAP to do same as C notation "var = i++"?

Sandra_Rossi
Active Contributor
0 Kudos

Hello experts,

In a loop, I have the following stupid code, I'd like to merge the 2 lines "ADD 1 TO d" and "struc2-fldate = d" (I used a date, but my question is valid for numeric types too) :

DATA struc2 type sflight.
DATA itab2 type table of sflight.
DATA(d) = sy-datum.

SELECT * FROM spfli INTO table @data(itab).

LOOP AT itab INTO data(struc).
  move-corresponding struc to struc2.

  ADD 1 TO d.   struc2-fldate = d.
  APPEND struc2 TO itab2. ENDLOOP.

The only solution I could find is to develop a method LCL_APP=>INC so that to do the following code:

struc2-fldate = lcl_app=>inc( CHANGING counter = d ).

The method is defined as follows:

CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS inc CHANGING var TYPE any RETURNING VALUE(counter) TYPE decfloat34.
ENDCLASS."

CLASS lcl_app IMPLEMENTATION.
  METHOD inc.
    ADD 1 TO var.
    counter = var.
  ENDMETHOD."
ENDCLASS."

Is there a simpler way?

Thanks!

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

The obstacles fort introducing such "a small function" in old ABAP are rather high (name clashes). I asked for those already but without success.

21 REPLIES 21

Former Member
0 Kudos

Hi Sandra,

You can write like this within loop,

STRUC2-FLDATE = SY-DATUM + 1.

0 Kudos

This will not replace Sandra's code as in each row there will be fldate = sy-datum + 1, but it should be sy-datum + 1 in the first, sy-datum + 1 +1 in the second step and so on...

ŁukaszPęgiel
Contributor
0 Kudos

We don't have replacement for i++ in ABAP so your way with the method seems to be only if you'd like to get rid of one line.

But maybe this is something for Dev team of SAP to add such small but useful function ?

0 Kudos

By the way. the practical usage is that our database team has defined 5 database tables with an increment counter as last field of the primary key to differentiate the lines, so I have an ugly repetition of the 2 lines (ADD 1 and assignment) at every database table processing, and I wondered whether this "simple" notation was possible (with all new beautiful constructs in 7.40).

Thanks. So, I will keep the ADD 1.

0 Kudos

Maybe something with REDUCE and / or FOR i =... UNTIL could be done here, but would it make it simpler and in one line ? I doubt.

0 Kudos

Why not. As I'm looking for just a little bit prettier code, with the help of "FOR ... INDEX INTO ..." (prettier than the old SY-TABIX), I think it does the trick.

Before:

        wlv_seq = 0.
        LOOP AT <lfs_cond_temp>-plage_horaire ASSIGNING FIELD-SYMBOL(<lfs_plage_horaire>).

          CLEAR wls_zcd_class_tempty.
          wls_zcd_class_tempty-zcode_calendrier = ws_input->calendrier-code_calendrier.
          wls_zcd_class_tempty-zdat_deb         = ws_input->calendrier-periode_validite-date_de_debut_de_validite.
          ADD 1 TO wlv_seq.
          wls_zcd_class_tempty-znb_id_jour      = wlv_seq.
          wls_zcd_class_tempty-zid_jour         = <lfs_jour_type>-jour_type.
          wls_zcd_class_tempty-zcode_cltemp     = <lfs_cond_temp>-code_classe_temporelle.
          wls_zcd_class_tempty-zduree_min       = <lfs_plage_horaire>-duree_minimale.
          wls_zcd_class_tempty-zduree_max       = <lfs_plage_horaire>-duree_maximale.
          wls_zcd_class_tempty-zhr_deb          = <lfs_plage_horaire>-heure_de_debut.
          wls_zcd_class_tempty-zhr_fin          = <lfs_plage_horaire>-heure_de_fin.
          APPEND wls_zcd_class_tempty TO wlt_zcd_class_tempty.

        ENDLOOP.

After:

        wlt_zcd_class_tempty =
                VALUE #(  BASE wlt_zcd_class_tempty
                          FOR <lfs_plage_horaire> IN <lfs_cond_temp>-plage_horaire INDEX INTO i
                          ( VALUE #(
                                    zcode_calendrier = ws_input->calendrier-code_calendrier
                                    zdat_deb         = ws_input->calendrier-periode_validite-date_de_debut_de_validite
                                    znb_id_jour      = i
                                    zid_jour         = <lfs_jour_type>-jour_type
                                    zcode_cltemp     = <lfs_cond_temp>-code_classe_temporelle
                                    zduree_min       = <lfs_plage_horaire>-duree_minimale
                                    zduree_max       = <lfs_plage_horaire>-duree_maximale
                                    zhr_deb          = <lfs_plage_horaire>-heure_de_debut
                                    zhr_fin          = <lfs_plage_horaire>-heure_de_fin
                                    ) )
                          ).

Thanks Łukasz.

0 Kudos

Hi Sandra,

I think Łukasz meant using REDUCE/FOR to replicate the behaviour of the i++ operator

From my experience i can say that FOR/REDUCE operators are really great to write cleaner code. I only wish there was an EXIT addition to the FOR operator

BR,

Suhas

0 Kudos

Exactly, I love to use FOR (REDUCE I can't so far ) as well to make the code cleaner, but for i++ it would not make it cleaner I guess

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

The obstacles fort introducing such "a small function" in old ABAP are rather high (name clashes). I asked for those already but without success.

0 Kudos
introducing such "a small function" in old ABAP

Hi Horst,

What do you mean by "old" ABAP here?

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

It seems that introducing new operators with natural names can easily invalidate existing programs.

An we don't want awkward names.

0 Kudos

Then maybe a class with some static methods  would be a solution? Of course with short name of the class and methods instead   Not a perfect solution but could be helpful. Of course this could also be done by the community if someone would collected needed methods

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Wouldn't it be simpler (and faster) to leave it as

d = d + 1. 

struc2-fldate = d.

then?

0 Kudos

In this case, yes

0 Kudos

Indeed - ABAPers' first world problems

0 Kudos

OP asked

I'd like to merge the 2 lines

Is ()


struc2-fldate = d = d + 1.

Still correct as executed from right to left? (and one single line as original requirement)

Regards,

Raymond

0 Kudos

hehe, so obvious that almost forgotten

0 Kudos

oh oh, good one !

Peter_Inotai
Active Contributor
0 Kudos

I know using MACRO is kind of old style and should be avoided, but in such cases I wouldn't have problem to use it.

In my previous company with had a bunch of macros for our framework and actually it made life easier. That could be also an option.

Peter

0 Kudos

Yes, it would be perfect, but for this code, it wouln't be pretty. If ABAP engineers could make a new constructor operator "macro" in next release : struc2-fldate = macro #( inc i ). and DEFINE inc. &1 = &1 + 1. &# = &1. END-OF-DEFINITION, with &# being the auxiliary variable to return. The arguments in macro being the name of the macro followed by the arguments. The compiler would include the macro code before the statement