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

Search in Internal Table Utility -- Need Pointers

Former Member
0 Likes
448

Hi Experts,

As per my requirement i need to search for words in internal table and arrange internal table based on ranking i.e record which has maximum no. of hits should come first.

Example: search for a string 'media rent'.

Incoming internal table records.

row1------> col1: rent col::xyz

row2 -


> col1:media rent col2:media

row3 -


> col1: rent col2:media media

I need to compare each word in col1 and col2 and arrange the rows based on ranking like this:

final internal table

row2 because it has maximum no. of search term

row3

row1

I found out 2 ways to do the above but facing some issues and need your suggestions:

Solution 1:

By using regualr expression i am searching as OR expression on itab.

and using FIND ALL OCCURRENCES OF REGEX command on each column of internal table

Issue: when row3 is scanned for 'media rent' it will give 3 counts i which will lead to move row3 on top in final table due to maximum no of counts.

Is there a way to neglect 2nd occurence in the word ? I cn't use find first occurence here.

Solution 2:

Split search sting 'media rent' into internal table

itab1

media

rent

and search for each word in each row of internal table and make use of counter something like this.i am unable to proceed further here

I am stucked with what kind of logic should be here ?

Looking for your suggestions here.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
412

Hi fried,

Try the code below it works fine.

TYPES : BEGIN OF ty_text,

text1 type char11,

text2 TYPE char11,

count TYPE i,

END OF ty_text.

data : t_text TYPE TABLE OF ty_text,

x_text TYPE ty_text.

DATA : v_count TYPE i.

x_text-text1 = 'rent'.

x_text-text2 = 'xyz'.

append x_text to t_text.

x_text-text1 = 'media'.

x_text-text2 = 'media'.

append x_text to t_text.

x_text-text1 = 'rent'.

x_text-text2 = 'media media'.

append x_text to t_text.

loop at t_text INTO x_text.

if x_text-text1 eq 'media' or x_text-text1 eq 'rent'.

v_count = v_count + 1.

endif.

if x_text-text2 eq 'media' or x_text-text2 eq 'rent'.

v_count = v_count + 1.

endif.

x_text-count = v_count.

MODIFY t_text INDEX sy-tabix FROM x_text.

clear v_count.

ENDLOOP.

SORT t_text DESCENDING by count.

If you face any issues please revert back to me i will help you.

Thanks,

Sri Hari

2 REPLIES 2
Read only

Former Member
0 Likes
413

Hi fried,

Try the code below it works fine.

TYPES : BEGIN OF ty_text,

text1 type char11,

text2 TYPE char11,

count TYPE i,

END OF ty_text.

data : t_text TYPE TABLE OF ty_text,

x_text TYPE ty_text.

DATA : v_count TYPE i.

x_text-text1 = 'rent'.

x_text-text2 = 'xyz'.

append x_text to t_text.

x_text-text1 = 'media'.

x_text-text2 = 'media'.

append x_text to t_text.

x_text-text1 = 'rent'.

x_text-text2 = 'media media'.

append x_text to t_text.

loop at t_text INTO x_text.

if x_text-text1 eq 'media' or x_text-text1 eq 'rent'.

v_count = v_count + 1.

endif.

if x_text-text2 eq 'media' or x_text-text2 eq 'rent'.

v_count = v_count + 1.

endif.

x_text-count = v_count.

MODIFY t_text INDEX sy-tabix FROM x_text.

clear v_count.

ENDLOOP.

SORT t_text DESCENDING by count.

If you face any issues please revert back to me i will help you.

Thanks,

Sri Hari

Read only

0 Likes
412

Closing the thread. Solved by myself.

Thanks sri for your inputs.