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

Binary Search

Former Member
0 Likes
579

Hi All,

data: begin of t_kna1 occurs 0,

kunnr like kna1-kunnr,

name1 like kna1-name1,

end of t_kna1.

select kunnr name1 from kna1 into table t_kna1.

sort t_kna1 by kunnr name1.

loop at t_bseg.

read table t_kna1 with key kunnr = t_bseg-kunnr name1 = t_bseg-name1

binary search.

............

endloop.

loop at t_bseg.

read table t_kna1 with key name1 = t_bseg-name1

kunnr = t_bseg-kunnr binary search.

............

endloop.

In the above sample code we are reading the table t_kna1 with different key sequence.What is difference in that which should be followed why? whether the second one is wrong or what?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
553

Hi Sri ram,

there might be littel difference in reading the records from the internal table "<b>t_kna1</b> " for the second and first case as the key sequence in the read statement is different....

In the <b>first case</b> the records are read firstly based upon the kunnnr and then based upon the name field...

whereas in the <b>second case</b> as per the key sequnece in the read statement

the filtering of the read statement is done initially by the name 1 field and then the kunnr field.

Regards ,

Ginni.

Reward if helpful.

5 REPLIES 5
Read only

h_senden2
Active Contributor
0 Likes
553

Hi,

you can only use the binary search for fields on which the table is sorted.

So the first READ TABLE is working, the second IS NOT !

You can add a SORT T_KNA BY NAME1 KUNNR before the second loop-statement.

regards,

Hans

Please reward all helpful answers !!!!!

Read only

Former Member
0 Likes
553

Hi,

In both cases no need to use NAME1 field as key field to read, as it is a description field.

Right thing is:

read table t_kna1 with key kunnr = t_bseg-kunnr binary search.

binary search reads data fast.

But you must sort the internal table first with that field.

reward points if useful

regards,

ANJI

Read only

Former Member
0 Likes
553

Hi Shri,

It depends on the requirement for searching in the internal table..

Both are correct but you have to sort for that particular fields which you are using as key.( not to sort all the fields when one is required..)

Read only

Former Member
0 Likes
553

Hi,

it will be better if u write like first statement, bcoz it is as per standard.

Hope this will help you for this and for your previous post.

Award all helpful answers and close the post as answered.

Read only

Former Member
0 Likes
554

Hi Sri ram,

there might be littel difference in reading the records from the internal table "<b>t_kna1</b> " for the second and first case as the key sequence in the read statement is different....

In the <b>first case</b> the records are read firstly based upon the kunnnr and then based upon the name field...

whereas in the <b>second case</b> as per the key sequnece in the read statement

the filtering of the read statement is done initially by the name 1 field and then the kunnr field.

Regards ,

Ginni.

Reward if helpful.