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 problem

Former Member
0 Likes
806

a binary search see below is returning a sy-subrc code of 8 and not finding the record as i expect. anyone know why?

in debug i can see the entry in the internal table. it just doesn't find it using the binary search. the values are correct in the select statement and i don't think there is a problem with formatting. there are approx 700,000 entries in the table. the dis_ch (result) field is empty and should be populated with the bussct field.

<b>internal table declaration</b>

TYPES: BEGIN OF s_custsa,

salorg type /BIC/PIO_CUSTSA-/BIC/IO_SALORG,

dis_ch type /BIC/PIO_CUSTSA-/BIC/IO_DIS_CH,

divs type /BIC/PIO_CUSTSA-/BIC/IO_DIVS,

custsa type /BIC/PIO_CUSTSA-/BIC/IO_CUSTSA,

bussct type /BIC/PIO_CUSTSA-/BIC/IO_BUSSCT,

END OF s_custsa.

DATA: it_pio_custsa TYPE STANDARD TABLE OF s_custsa with header line

WITH NON-UNIQUE DEFAULT KEY INITIAL SIZE 10.

<b>filling the internal table with all active records.</b>

SELECT /BIC/IO_SALORG /BIC/IO_DIS_CH /BIC/IO_DIVS /BIC/IO_CUSTSA

/BIC/IO_BUSSCT

FROM /BIC/PIO_CUSTSA

INTO table it_pio_custsa

WHERE OBJVERS = 'A'.

delete it_pio_custsa where bussct = ''.

sort it_pio_custsa by custsa salorg divs.

<b>now try to find the entry</b>

read table it_pio_custsa with key

SALORG = COMM_STRUCTURE-/BIC/IO_SALORG

DIVS = COMM_STRUCTURE-/BIC/IO_DIVS

CUSTSA = COMM_STRUCTURE-/BIC/IO_CUSTSA binary search.

if sy-subrc = 0.

RESULT = it_pio_custsa-bussct.

endif.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
760

Hi,

If u want to retain the read statement as it is,

then sort the table by -

sort it_pio_custsa by salorg divs custa.

rather than -

sort it_pio_custsa by custsa salorg divs.

it makes sense to change the sort sequence because data type for internal table is defined in the same order.

5 REPLIES 5
Read only

Former Member
0 Likes
760

read internal table as

read table it_pio_custsa with key

CUSTSA = COMM_STRUCTURE-/BIC/IO_CUSTSA

SALORG = COMM_STRUCTURE-/BIC/IO_SALORG

DIVS = COMM_STRUCTURE-/BIC/IO_DIVS

binary search.

if sy-subrc = 0.

RESULT = it_pio_custsa-bussct.

endif.

order should be same as in sort.

did you got it

regards

Read only

Vinod_Chandran
Active Contributor
0 Likes
760

Hi,

Not sure whether this will work. But worth trying...

The order of fields given in SORT and READ TABLE is different.

sort it_pio_custsa by custsa salorg divs.

read table it_pio_custsa with key

CUSTSA = COMM_STRUCTURE-/BIC/IO_CUSTSA

SALORG = COMM_STRUCTURE-/BIC/IO_SALORG

DIVS = COMM_STRUCTURE-/BIC/IO_DIVS

binary search.

if sy-subrc = 0.

RESULT = it_pio_custsa-bussct.

endif.

Thanks

Vinod

Read only

Former Member
0 Likes
761

Hi,

If u want to retain the read statement as it is,

then sort the table by -

sort it_pio_custsa by salorg divs custa.

rather than -

sort it_pio_custsa by custsa salorg divs.

it makes sense to change the sort sequence because data type for internal table is defined in the same order.

Read only

0 Likes
760

thanks all for your comments. what i find strange is that out of a sample debug of 140 records only 3 fail to be found using the original code. why would this be the case?

i will amend the code as suggested keeping the internal table and read formats as they correspond to the master data table that is being read from. and just change the sort sequence.

i'll award some points tomorrow if it works!!

Read only

Former Member
0 Likes
760

HI

do the sorting as per in the read statement

sort it_pio_custsa by custsa salorg divs

instead of the above statement change it like

sort it_pio_custsa by salorg divs custsa.

Cheers,

Sasi