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

reab table- binary search

Former Member
0 Likes
806

Hi,

i have an internal table:

types: begin of ty_mara,

matnr type matnr,

werks type werks_d,

lgort type lgort,

menge type menge,

end of ty_mara.

data: wa_mara type ty_mara,

gt_mara type table of ty_mara with default key initial size 10.

now i fill this gt_mara with data using select statement on different tables...

let's suppose i get 50000 records into table

sort gt_mara by matnr werks lgort.

read table gt_mara into wa_mara with key matnr = '10000678'

werks = 'CA01'

lgort = 'LT45'

BINARY SEARCH.

if the internal table has 1000 (duplicate)entries with same key, will the read statement with binary search option bring the 1st record of this 1000 records? Is it assured?

Note: I don't want to delete duplicate entries

Thanks,

Ravi

6 REPLIES 6
Read only

former_member209217
Active Contributor
0 Likes
706

Hi Ravi,

Yes it will pick up the first record encountered in the internal table.But how do you know that the record picked first is the one you are looking for?Because you are saying that you don't want to delete duplicate entries.Maintaining duplicate entries causes inconsistency.

Regards,

Lakshman.

Read only

Former Member
0 Likes
706

Hi ,

Yes your assumption is absolutely correct. It will always bring the very first matching record.

Now you are saying that in this case you dont want to loose the remaining same records. Is it so ? If yes then ,

That mean you want to read all the 50000 records present in the table.

Then Read them on the index basis.

Read table into wa index counter.

READ TABLE tab INDEX sy-index/counter INTO wa.

IF sy-subrc = 0.

--

ENDIF.

increment counter every time.

Hope this is helpful.

Regards,

Uma Dave

Edited by: UmaDave on Jun 22, 2010 7:21 AM

Edited by: UmaDave on Jun 22, 2010 7:26 AM

Read only

Former Member
0 Likes
706

Hello Ravi,

Let me explain Read statements in detail:

1) Read table intenal_table into work_area with key = (the corresponding key).

Here the table is searched upon this key. Upon finding the record with the specified key the sy-subrc is set to 0 and you process the record.

This is mainly used to find if there exists a record with the search parameter you are passing in the read statement.

2) Sort internal_table.

Read table intenal_table into work_area with key = (the corresponding key) binary search.

Here too you search for a pariticular record with the search key but the search happens faster as you are sorting the internal table and you are searching based on BINARY SEARCH OPTION.

3) If the intention is only to search fields and if no operation is to be performed please use this:

Read table intenal_table into work_area with key = (the corresponding key) binary search transporting no fields.

This would yield the most responsive search result.

That was about the read statements. Now coming to your queires, Yes it access the first record of the search parameter that satisifies the provided keys and to get to a particular record you need to pass the exact search key in order to avoid the loop and be specific interms of searching the particular record (Exact searh criterion).

Hope this helps.

Best Regards,

Shree Tejus.

Read only

Former Member
0 Likes
706

hi,

binary search will always return the first record in the internal table matching the search criteria. you can sort table by char or number or numeric fields and binary search will always return the first record if there are duplicates

Read only

Former Member
0 Likes
706

Hi Ravi,

You cant be certain which record will be picked.It will bring any record matching the criterion.Because in simple read statement,sequential search is performed.But logic for binary search is different.Binary search repeatedly divides the search interval in half. If the value to be searched is less than the item in the middle of the interval, the search is narrowed to the lower half, otherwise the search is narrowed to the upper half. So any record can be picked.

Hope it helps

Thanks & Regards,

Vivek Kumar

Read only

Former Member
0 Likes
706

This message was moderated.