2013 Feb 11 4:30 PM
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
2013 Feb 11 4:53 PM
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
2013 Feb 11 4:53 PM
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).
2013 Feb 11 5:54 PM
2013 Feb 11 5:02 PM
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.
2013 Feb 11 5:56 PM
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
2013 Feb 12 2:31 PM
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
2013 Feb 13 10:20 AM
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
2013 Feb 13 10:25 AM
You can simply use an offset on lv_string to consider only the part excluding the first column.
2013 Feb 13 1:25 PM
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
2013 Feb 13 2:39 PM
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.
2013 Feb 13 4:03 PM
Thanks.
I will test the different possibities in my system.
Amine
2013 Feb 13 4:07 PM
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
2013 Feb 13 5:23 PM
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |