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

Manage Lines Vs Columns

Former Member
0 Likes
1,656

Hi experts,

I would perform a test in abap but i’m stuck. In fact, I don’t know how to manage lines versus columns.

Here my test:

COL1

COL2

TEST

X

TEST2

TEST3

X

X

What I would like to do is to check that each element in blue has at least an ‘X’ checked in the green columns. For example line TEST 2 doesn’t have any ‘X’ checked.

How can I manage this?

I thought about building an internal table as in my example ans may be do a sort somewhere to extract the empty ones….

If someone has a better idea than mine, it’s welcome.

Thanks.


Amine

1 ACCEPTED SOLUTION
Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,624

Since you have just two columns in your example table, it's possible to simply use a LOOP with WHERE condition:

LOOP AT itab WHERE COL1 = 'X' OR COL2 = 'X'.

Or, you can even use the DELETE statement to get rid of the rows where no column is 'X':

DELETE FROM itab WHERE COL1 = space AND COL2 = space.

But, if you are looking at a more generic solution, you can do something like below.

LOOP AT itab INTO struc.

   lv_string = struc.     " lv_string is a string

   CHECK lv_string CS 'X'.

ENDLOOP.

Do note that the assignment of a structure to a string variable would work only if the structure contains all character-type fields (which seems to be the case in your problem).

Hi experts,

I would perform a test in abap but i’m stuck. In fact, I don’t know how to manage lines versus columns.

Here my test:

COL1

COL2

TEST

X

TEST2

TEST3

X

X

What I would like to do is to check that each element in blue has at least an ‘X’ checked in the green columns. For example line TEST 2 doesn’t have any ‘X’ checked.

How can I manage this?

I thought about building an internal table as in my example ans may be do a sort somewhere to extract the empty ones….

If someone has a better idea than mine, it’s welcome.

Thanks.


Amine

12 REPLIES 12
Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,625

Since you have just two columns in your example table, it's possible to simply use a LOOP with WHERE condition:

LOOP AT itab WHERE COL1 = 'X' OR COL2 = 'X'.

Or, you can even use the DELETE statement to get rid of the rows where no column is 'X':

DELETE FROM itab WHERE COL1 = space AND COL2 = space.

But, if you are looking at a more generic solution, you can do something like below.

LOOP AT itab INTO struc.

   lv_string = struc.     " lv_string is a string

   CHECK lv_string CS 'X'.

ENDLOOP.

Do note that the assignment of a structure to a string variable would work only if the structure contains all character-type fields (which seems to be the case in your problem).

Read only

Former Member
0 Likes
1,624

Thanks Kumar.

Amine

Read only

former_member491621
Contributor
0 Likes
1,624

Hi Amine,

The solution you are trying is probably one of the best options available.

Just 1 question(out of curiosity) - how many columns does your internal table have?? The sorting and extraction is dependent on that.

Read only

0 Likes
1,624

Hi Aniket,

I don't know yet. I am just preparing some tests in order to have ideas for some future requirments.

But, it will be for sure more than 2 columns (10 at least )

Amine

Read only

0 Likes
1,624

Hi Amine,

You may first sort the internal table and then delete the data from it.

Or after sorting on all the columns,

SORT itab1 STABLE ASCENDING BY f1 f2 ...fn. (where n is the last column)

you can find the index of the first row where the last column has a value X(fn = 'X')

and then append the lines of itab1 to another itab of the same type(just in case you want to have the required data for further processing).

APPEND LINES OF itab1 FROM from_indx TO to_indx TO itab2. (from_indx = 1 and to_indx from above statement).

The appending method works faster than - looping and moving individual values - as well as delete statement.

Hope this helps

Read only

Former Member
0 Likes
1,624

Hi all,

I would like to understant the option proposed by Kumar better:

LOOP AT itab INTO struc.

   lv_string = struc.     " lv_string is a string

   CHECK lv_string CS 'X'.

ENDLOOP.

If i understand all, i will create a structure like the table in my example:

COL1

COL2

TEST

X

TEST2

TEST3

X

X

Me what i want to check, is if therie is an ‘X’ in the columns in Green and extract the lines which don’t have an X.

But if I change a little bit my example:


COL1

COL2

TEST

X

TESTX

TEST3

X

X

With the above code, record 2 will be considered as containing X and not extracted, right?

Thanks.

Amine


Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,624

You can simply use an offset on lv_string to consider only the part excluding the first column.

Read only

0 Likes
1,624

Hi Kumar,

If the number of my columns is 7 for example, i will use something like this right?

CHECK lv_string+1(6) CS 'X'.  

I have another concern:

   lv_string = struc.     " lv_string is a string 

that means regarding my example that:

lv_string = COL1COL2?

Thanks.

Amine

Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
1,624

Hi Amine,

You can check SAP help on offsets and it will be clear.

lv_string+p(q) would mean q characters of lv_string after the pth character. So, if your first column's length is 7 characters, then you need to do lv_string+7.

Regarding your other concern, yes, lv_string would contain contents of all the columns put together.

Read only

0 Likes
1,624

Thanks.

I will test the different possibities in my system.

Amine

Read only

0 Likes
1,624

Hi Amine,

Assuming that all the columns of your internal table are of type C, i could help you to understand the suggestion given by Akshat.

TYPES: BEGIN OF ty_tab,

               col1(1) TYPE c,

               col2(1) TYPE c,

             END OF ty_tab.

DATA : itab TYPE TABLE OF ty_tab,

           wa   TYPE ty_tab.

LOOP AT itab INTO wa.

     lv_string = wa.          " WA is flat char like structure

     CHECK lv_string CS 'X'.

          ....perform required operations

ENDLOOP.

Here, the WA can be considered as a long, char type variable with length equal to the sum of lengths of all fields in the work area(i.e in this case length = 2).

When you assign the WA to lv_string, you are assigning a character type variable to a string type variable, you are doing nothing different than assigning one char type variable to another char type variable(where length = undefined).

Assigning WA to lv_string with any number of fields is possible in this case.

You can also try and use CHECK lv_string IS INITIAL instead of CHECK lv_string CS 'X'.

Both have the same effect.

Hope this helps

Read only

0 Likes
1,624

In fact, i understood

Thanks for your explanations.

Amine