Application Development and Automation 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: 
Read only

Case Statement Dynamic

Former Member
0 Likes
7,149

Dear All,

My requirement is as below:

Case var.

when 'A' or 'B' or 'C'.

  "do something with letters".

when '1' or '2'.

" Do something with number.

endcase.

now, I want to add "D" also but everytime new letter has to be added, I need to make changes in Code like

when "A" or "B" or"C" or "D".

"do something with letters".


Can we have solution, where we just add D in any field in table and when keyword uses that for usage.


I tried it by passing all values in table and perform it, but "OR" keyword is not been read as it has to be.

Please do let me know if we can achieve this.


Thanks n advance.


Regards,

Sitesh

Dear All,

My requirement is as below:

Case var.

when 'A' or 'B' or 'C'.

  "do something with letters".

when '1' or '2'.

" Do something with number.

endcase.

now, I want to add "D" also but everytime new letter has to be added, I need to make changes in Code like

when "A" or "B" or"C" or "D".

"do something with letters".


Can we have solution, where we just add D in any field in table and when keyword uses that for usage.


I tried it by passing all values in table and perform it, but "OR" keyword is not been read as it has to be.

Please do let me know if we can achieve this.


Thanks n advance.


Regards,

Sitesh

15 REPLIES 15
Read only

former_member259807
Active Participant
0 Likes
3,960

How about switch from CASE to IF and use selection table (operand [NOT] IN seltab) ?

Read only

ziolkowskib
Active Contributor
0 Likes
3,960

Have you considered using IF. ELSE. with ranges?

What I mean is the following:

IF var1 IN lr_range_1.

....

ELSEIF var1 IN lr_range_2,

...

...

ENDIF.

You could simply populate range content dynamically.

Read only

Former Member
0 Likes
3,960

Dear All,

With IF Elseif condition, it is possible.

The code is already implemented and I do not want to make more changes.

Plus, there are many when (option) command.

If we use if elseif, it is suitable with only 2 conditions. for more than 2-3 condition, we should not use IF statement but Case statement works faster and better.

Hence, I am trying to find the solution with Case statement only.

Thanks n regards,

Sitesh

Read only

0 Likes
3,960

Sitesh,

In the online documentation for keyword CASE, the use of table operations is expressly ruled out (See bulletpoint 2 below.).  Since a RANGE is an internal table, that would seem to rule out using ranges as the WHEN operand in a CASE statement.

Loyd

Read only

0 Likes
3,960

Sitesh Kumar said:

for more than 2-3 condition, we should not use IF statement but Case statement works faster and better.

Faster ? I'd like to see the benchmark ! (probably, in a loop of 1 trillion elements, no doubt that there will be a gain of 1 microsecond)

Better ? I guess you mean that CASE is a more human-readable format, and it's a faster input and avoids wrong inputs (while retyping the same variable in IF). Agreed.

Read only

matt
Active Contributor
0 Likes
3,960

Use functional methods and IF... ELSEIF...

IF is_numeric( var ).

  " Do numeric stuff

ELSEIF is_letters( var ).

  " Do letter stuff.

ELSE.

   " Do something else.

ENDIF.

Or better, instead of var as just a value, make it an object. Then, with polymorphism, you can simply use

var->do_appropriate_stuff( ).

Read only

Clemenss
Active Contributor
0 Likes
3,960

Case abap_true.

   When is_letter( var ).

     Do someting for letters.

   When is_numeric( var ).

     Do someting for numbers.

Endcase.

Regards Clemens

Looks strange but works so nice

Read only

Former Member
0 Likes
3,960

Hi Satish,

You can use Scalability table. A Scalability table is a Z - Table where values are maintained for additional cases like your scenario to avoid code changes.

For example let the structure of the Scalability table(Z_SCAL) be -

Variable | count | value | Type

Maintain data where for all numeric values use type as Numeric and for all alphabets use type as Alpha.

Read the data into two internal Tables i_num i_alpha.

select * from Z_SCAL where into table i_num where type EQ 'NUMERIC'.

select * from Z_SCAL where into table i_alpha where type EQ 'ALPHA'.

Up on checking Read the internal table for data.

Read table i_alpha where value EQ v_var.

if sy-subrc EQ 0.

******* do some thing for alpha.

else if sy-subrc NE 0.

Read table i_num where value EQ v_var.

****** do some thing for Numbers.

else

********* do something else.

end if.

By this upon maintaining data in Z_SCAL entries your case can be solved avoiding code changes.

This can also be used for not only alpha and numeric. Even 1 2 A b as one Group and A B 5 6 as another group based on how you are grouping the variables in the type column.

Read only

0 Likes
3,960

Hi,

I guess there is some misunderstanding here. I gave an example.

The requirement is like:

Case VAR.

when 'AC' or 'HP' or 'FC'.

  Do something.

when 'QC' or 'TC'.

Do something.

endcase.

I have to add 'PC' along with 'FC' in above when condition.

There is no number or alphabet concept.

I wanted if I can use as below:

Select field from ztable where (condition).

pass value in VAR.

Case VAR.

When "1st condition" (This would include all like AC or HP or FC, I just need to add value in table)

  do something

when "2nd condition"

Do.

endcase.

Mr. Loyd has hinted me well with his answer. Thank you Mr. Loyd. Even I found that we can not use table operation with Case statement.

Thanks n Regards,

Sitesh

Read only

YaswanthDatta
Explorer
0 Likes
3,960

Hi Sitesh,

     You can maintain these entries in a Z table and then fetch them during runtime, concatenate them into a string also using a separator. Then use the CASE statement as below:

Case VAR

     When VAR CS lv_string1     

     ..............

     ..............

     When VAR CS lv_string2.

     ..............

     ..............

Endcase.

I used Contains String operator to check complete existence of VAR in string 1.


-Yaswanth Datta



Read only

0 Likes
3,960

Hi Yaswanth,

this is not possible. Can we use Bit operator in When statement. I dont think so.

Please send me working code if it is possible.

Thanks n Regards,

Sitesh

Read only

matt
Active Contributor
0 Likes
3,960

Yaswanth Datta Vathumilli wrote:

Hi Sitesh,

     You can maintain these entries in a Z table and then fetch them during runtime, concatenate them into a string also using a separator. Then use the CASE statement as below:

Case VAR

     When VAR CS lv_string1    

     ..............

     ..............

     When VAR CS lv_string2.

     ..............

     ..............

Endcase.

I used Contains String operator to check complete existence of VAR in string 1.


-Yaswanth Datta



When var CS string is not valid ABAP. Why are you posting this?

Read only

matt
Active Contributor
0 Likes
3,960

What Yaswanth posted is not valid ABAP. I think perhaps he's guessing at a solution.

Read only

0 Likes
3,960

HI Sitesh,

CS is string operator, but yes it doesn't work with Case statement. But the same logic works with If - Elseif logic. PFB the logic I tried. You can store your variables in any table, fetch them in runtime  and place them in in different internal tables and then use them as below:

LOOP AT li_variables1 INTO wa_variable.

   IF lw_string1 IS INITIAL.

     lw_string1 = wa_variable.

   ELSE.

     CONCATENATE lw_string1 wa_variable INTO lw_string1.

   ENDIF.

ENDLOOP.

LOOP AT li_variables2 INTO wa_variable.

   IF lw_string2 IS INITIAL.

     lw_string2 = wa_variable.

   ELSE.

     CONCATENATE lw_string2 wa_variable INTO lw_string2.

   ENDIF.

ENDLOOP.

IF var CS lw_string1.

   "Do something

ELSEIF var CS lw_string2.

   "Do something else

ENDIF.

- Yaswanth Datta

Read only

matt
Active Contributor
0 Likes
3,960

Using IF ELSEIF has already been suggested. I think as we're unlikely to get any new answers, I'm locking this thread.