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

Read Internal Table

Former Member
0 Likes
630

Hello All,

I need to read a particular record from an internal table. Can you please help me build the logic?

Here's the scenario: I have Order Number and a flag as fields in an internal table. I need to pick the Order Number which is not flagged and next to the highest Order is which is flagged. Ignore anything in the middle. I know that I can get this done by creating another internal table. But was wondering how I do it using only 1 internal table.

In the following example, I need to pick Order 8450 because the highest flagged order is 7020.

Thanks a lot.

ITAB:

________

Order|Flag

9460|

8450|

7020|X

6030|X

5120|

5068|X

4320|X

2130|

1150|X

Thanks.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
602

Something along the lines of:

READ TABLE itab WITH KEY flag = 'X'.
index = sy-tabix - 1.
READ TABLE itab INDEX index.

I tested this and it does what I want it to do.

Rob

Edited by: Rob Burbank on Oct 24, 2008 5:31 PM

3 REPLIES 3
Read only

Former Member
0 Likes
603

Something along the lines of:

READ TABLE itab WITH KEY flag = 'X'.
index = sy-tabix - 1.
READ TABLE itab INDEX index.

I tested this and it does what I want it to do.

Rob

Edited by: Rob Burbank on Oct 24, 2008 5:31 PM

Read only

0 Likes
602

try to do that:

DATA: BEGIN OF itab OCCURS 0,
        order(10),
        flag,
      END OF itab.

DATA: w_order LIKE itab-order,
      w_tabix TYPE sy-tabix..

itab-order = 9460.
itab-flag  = ' '.
APPEND itab.

itab-order = 8450.
itab-flag  = ' '.
APPEND itab.

itab-order = 7020.
itab-flag  = 'X'.
APPEND itab.

itab-order = 6030.
itab-flag  = 'X'.
APPEND itab.

itab-order = 5120.
itab-flag  = ' '.
APPEND itab.

itab-order = 5068.
itab-flag  = 'X'.
APPEND itab.

itab-order = 4320.
itab-flag  = 'X'.
APPEND itab.

itab-order = 2130.
itab-flag  = ' '.
APPEND itab.

itab-order = 1150.
itab-flag  = 'X'.
APPEND itab.

SORT itab BY order DESCENDING.

CLEAR w_tabix.
LOOP AT itab.
  IF  w_tabix IS INITIAL
  AND itab-flag EQ 'X'.
    w_tabix = sy-tabix.
  ENDIF.
ENDLOOP.

w_tabix = w_tabix - 1.

READ TABLE itab INDEX w_tabix.
WRITE: itab-order.

Read only

0 Likes
602

Great Rob, Thanks. That worked.