‎2011 Dec 21 4:14 AM
Hi Experts,
I am working on a static class which will be used in many programs to search for a phrase in internal table.
INPUT PARAMETERS of my class :
1.SEARCH_STRING type string: This parameter is used to pass search string to search in ITAB
2.FIELD_NAMES type table of string: This parameter will be passed with field names .(this parameter tells in which fields of ITAB the search string needs to be searched.
3. ITAB type table.
OUTPUT Parameter:
IT_JOB_OUT type table
Below is my code:
LOOP AT ITAB ASSIGNING <FS_JOB_LIST>.
LV_INDEX = SY-TABIX.
CLEAR:LV_F1,LV_F2,LV_F3,LV_F4,LV_F5,LV_F6,LV_F7,LV_F8,LV_TOTAL.
LOOP AT FIELD_NAMES ASSIGNING <FIELD_NAME>.
ASSIGN COMPONENT <FIELD_NAME> OF STRUCTURE <FS_JOB_LIST> TO <FIELD>.
FIND ALL OCCURRENCES OF SEARCH_STRING
IN <FIELD> IN CHARACTER MODE
MATCH COUNT MCOUN.
IF SY-SUBRC = 0.
LV_F1 = LV_F1 + 1.
ENDIF.
ENDLOOP.
if LV_F1 > 0.
APPEND <FS_JOB_LIST> TO IT_JOB_OUT.
endif.
endloop.
Scenario:
I want to arrange the internal table IT_JOB_OUT based on ranking of search term.
Example:
SEARCH_STRING = 'TEST RANK'.
ITAB DATA
ROW 1 FIELD1 VALUE "ABC" FIELD2 VALUE "TEST RANK".
ROW 2 FIELD1 VALUE "TEST RANK" FIELD2 VALUE "TEST RANK".
The above code will give sy-subrc = 0 after find statement for both rows and result as
IT_JOB_OUT
ROW 1 FIELD1 VALUE "ABC" FIELD2 VALUE "TEST RANK".
ROW 2 FIELD1 VALUE "TEST RANK" FIELD2 VALUE "TEST RANK".
I want o/p as
ROW 1 FIELD1 VALUE "TEST RANK" FIELD2 VALUE "TEST RANK".
ROW 2 FIELD1 VALUE "ABC" FIELD2 VALUE "TEST RANK".
my problem is:
I want to arrange my output table such that maximum hits row should come first in above example my final table
should be ROW2 then ROW1. How can i achieve this ? Please note IT_JOB_OUT is of type table.
Please need your suggestions on this.
Thanks
Bhanu
‎2011 Dec 21 5:32 AM
Hi,
Why cant you use the SEARCH functionality provided by SAP to identify if there is a pattern available in the internal table.
SEARCH itab FOR pattern [IN {BYTE|CHARACTER} MODE]
[STARTING AT idx1] [ENDING AT idx2]
[ABBREVIATED]
[AND MARK].
The result of which populates the SY-TABIX with the row of the table and SY-FDPOS with the offset.
Thanks and Regards,
Sriranjani Chimakurthy.
‎2011 Dec 21 5:46 AM
Hi Sriranjani Chimakurthy
Search wont work here
1. SEARCH itab FOR pattern as you said will only work for itab which has all Char fields which is not the case.
2.itab FOR pattern will search for pattern not the phrase.
‎2011 Dec 21 5:44 AM
bhanu,
2 possible solutions.
1. store the LV_F1 as a column in the IT_JOB_OUT, so that once you come out of the second loop you can just sort the table IT_JOB_OUT descending by that LV_F1 field and bingo.
2. store the lv_f1 value in another variable to hold the value from previous loop.
and do a if statement like if lv_f1 > lv_old then insert at a higer index, else append.
but i think the first option is way easier that the second
‎2011 Dec 21 5:50 AM
Hi Soumyaprakash,
This is what i exactly thought but how can i add a column to IT_JOB_OUT which is of type table?
I am using generic types as my class will be used in many programs so the only way was to keep the internal table
generic.
But how can i add a column to my ITAB ??
Please suggest
Bhanu
‎2011 Dec 21 6:08 AM
define/Create a intermediate table with this column. pass it there, sort it there. then pass the required data to your final table. wont that do?
‎2011 Dec 21 6:26 AM
Thanks for the reply Soumyaprakash,
i was trying to create a internal table but what should be the type of my internal table???
The way you described it should work fine but what should be the structure of my ITAB?
LOOP AT IT_JOB_LIST_IN ASSIGNING <FS_JOB_LIST>.
LV_INDEX = SY-TABIX.
CLEAR:LV_F1,LV_F2,LV_F3,LV_F4,LV_F5,LV_F6,LV_F7,LV_F8,LV_TOTAL.
LOOP AT IT_FIELD_NAME ASSIGNING <FIELD_NAME>.
ASSIGN COMPONENT <FIELD_NAME> OF STRUCTURE <FS_JOB_LIST> TO <FIELD>.
FIND ALL OCCURRENCES OF I_SEARCH_STRING
IN <FIELD> IN CHARACTER MODE
MATCH COUNT MCOUN.
IF SY-SUBRC = 0.
LV_F1 = LV_F1 + 1.
ENDIF.
ENDLOOP.
IF LV_F1 > 0.
move <FS_JOB_LIST> to WA.
wa-count = LV_F1.
APPEND WA TO ITAB.
DELETE IT_JOB_LIST_IN INDEX LV_INDEX.
ENDIF.
‎2011 Dec 21 11:27 AM
Hi Bhanu,
This is what i exactly thought but how can i add a column to IT_JOB_OUT which is of type table?
Use RTTS class to build up your table structure dynamically and add the field you need...
I guess CL_ABAP_STRUCTDESCR should be enough for this... Once your structure is built, use a CREATE DATA statement to create the object and dereference it to a field symbol to be able to insert the record to your table.
Kr,
Manu.
‎2011 Dec 21 11:50 AM