01-26-2011 3:44 PM
Hi,
Can anyone advise if there is a way to check if a list of fields has value in it? I can write the if/then statement to check but I want to see whether there are other options.
Data: field_1 type menge_d,
field_2 type menge_d,
field_3 type menge_d,
field_4 type menge_d,
field_5 type menge_d,
field_6 type menge_d,
In ABAP code, these fields will get assigned a quantity. How can I check if to see if at least 4 of these six fieldts has data assigned to it. It can be in any combination but at least 4 fields should have data.
Currently, I have this code but want to see if there are other intelligent ways of doing it than these redundant IFs statements.
If field_1 > 0.
count = count + 1.
endif.
If field_2 > 0.
count = count + 1.
endif.
If field_3 > 0.
count = count + 1.
endif.
If field_4 > 0.
count = count + 1.
endif.
If field_5 > 0.
count = count + 1.
endif.
If field_6 > 0.
count = count + 1.
endif.
after this, I check if count > = 4
Thanks.
01-26-2011 4:47 PM
Hi Ravi,
you can build the fieldname dynamically in a loop and check for value not initial, i.e.
data:
lv_fieldname type fieldname,
lv_num1 type n length 1,
lv_count type int4.
field-symbols:
<any> type any.
DO 6 times.
add 1 to lv_num1.
concatenate 'FIELD_' lv_num1 into lv_fieldname.
assign (lv_fieldname) to <any>.
check:
sy-subrc = 0,
<any> is not initial.
add 1 to lv_count.
ENDDO.
Not much less than your code, but fully transparent and easy to adjust. LV-count has the number of values assigned.
Regards,
Clemens
01-26-2011 4:38 PM
You could:
use 6 times the form
perform f_check using field_1
changing count.
where the form contains the if.
otherwise you could:
field-symbols <field> type any.
data: field_data(10).
do 6 times.
concatenate 'field_' si-index into field_data.
assign (field_data) to <field>.
if <field> gt 0.
count = count + 1.
endif.
enddo.
regards.
andrea
01-26-2011 4:45 PM
01-26-2011 4:45 PM
You know what, for these six cases I would just leave it as is. You could reduce the number of lines somewhat by introducing constructions with field symbols or similar, but then your program becomes much more complex and difficult to understand and maintain.
-> "keep it simple, stupid", as much as possible and reasonable.
Thomas
01-26-2011 4:47 PM
Hi Ravi,
you can build the fieldname dynamically in a loop and check for value not initial, i.e.
data:
lv_fieldname type fieldname,
lv_num1 type n length 1,
lv_count type int4.
field-symbols:
<any> type any.
DO 6 times.
add 1 to lv_num1.
concatenate 'FIELD_' lv_num1 into lv_fieldname.
assign (lv_fieldname) to <any>.
check:
sy-subrc = 0,
<any> is not initial.
add 1 to lv_count.
ENDDO.
Not much less than your code, but fully transparent and easy to adjust. LV-count has the number of values assigned.
Regards,
Clemens
01-26-2011 6:40 PM
Assuming all the variables will have either a positive value or zero, the following would work. When variables can contain negative values, you will have to add ABS() within SIGN().
report y_test.
data: l_data1 type i VALUE 1,
l_data2 type i value 1,
l_data3 type i value 1,
l_data4 type i value 1,
l_data5 type i value 0,
l_data6 type i value 0,
l_result type i.
l_result = sign( l_data1 ) + sign( l_data2 ) +
sign( l_data3 ) + sign( l_data4 ) +
sign( l_data5 ) + sign( l_data6 ).
if l_result < 4.
write: / 'Less than 4'.
else.
write: / '4 or more'.
endif.
01-26-2011 9:19 PM
Hi Sudhi Karkada,
great idea!
As we can not use
sign( abs( l_data1 ) ).
before EHP2 (?), for the time being i'd like to suggest
l_result = sign( l_data1 ) * sign( l_data1 ) +
sign( l_data2 ) * sign( l_data2 ) +
sign( l_data3 ) * sign( l_data3 ) +
sign( l_data4 ) * sign( l_data4 ) +
sign( l_data5 ) * sign( l_data5 ) +
sign( l_data6 ) * sign( l_data6 ).
This way negative values ( -1-1) and positive values (11) will add 1 while initial (0*0) will add zero.
Regards,
Clemens