08-23-2016 11:02 AM
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!
08-23-2016 1:10 PM
The obstacles fort introducing such "a small function" in old ABAP are rather high (name clashes). I asked for those already but without success.
08-23-2016 11:39 AM
Hi Sandra,
You can write like this within loop,
STRUC2-FLDATE = SY-DATUM + 1.
08-23-2016 11:43 AM
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...
08-23-2016 11:45 AM
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 ?
08-23-2016 11:58 AM
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.
08-23-2016 12:08 PM
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.
08-23-2016 12:45 PM
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.
08-23-2016 12:57 PM
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
08-23-2016 1:05 PM
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
08-23-2016 1:10 PM
The obstacles fort introducing such "a small function" in old ABAP are rather high (name clashes). I asked for those already but without success.
08-23-2016 1:28 PM
introducing such "a small function" in old ABAP
Hi Horst,
What do you mean by "old" ABAP here?
08-23-2016 1:55 PM
It seems that introducing new operators with natural names can easily invalidate existing programs.
An we don't want awkward names.
08-23-2016 2:01 PM
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
08-23-2016 2:08 PM
Wouldn't it be simpler (and faster) to leave it as
d = d + 1.
struc2-fldate = d.
then?
08-23-2016 2:10 PM
08-23-2016 2:18 PM
08-23-2016 2:20 PM
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
08-23-2016 2:29 PM
08-23-2016 2:33 PM
08-23-2016 2:34 PM
08-23-2016 2:20 PM
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
08-23-2016 2:33 PM
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