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

find a specific value in Internal table

Former Member
0 Likes
3,392

Hallo experts,
I am trying to find a specific value 'material number' in an internal table , to do this I have wrote the code below , but it still missing me something, as I am receiving no results.

TYPES: BEGIN OF tab2,
lo_matnr TYPE matnr,
 END OF tab2.
DATA: BEGIN OF wa_tab1,
wa_matnr TYPE matnr,
 END OF wa_tab1,
 tbl_tab1 LIKE TABLE OF wa_tab1
                     WITH KEY wa_column1,
      tbl_tab1_result TYPE match_result_tab,
      wa_tab1_result LIKE LINE OF tbl_tab1_result.

wa_tab1-wa_matnr = wa_mara-matnr.
APPEND wa_tab1 TO tbl_tab1.
FIND ALL OCCURRENCES OF '20-30412' IN TABLE tbl_tab1
              RESULTS tbl_tab1_result IN CHARACTER MODE.
LOOP AT tbl_tab1_result INTO wa_tab1_result.
  READ TABLE tbl_tab1 INTO wa_tab1 INDEX wa_tab1_result-line.
  IF sy-subrc = 0.
    WRITE:/ 'Line:', wa_tab1_result-line, 'Offset:', wa_tab1_result-offset.
    WRITE: 'Value is:',  wa_tab1+wa_tab1_result-offset(3).
  ENDIF.
ENDLOOP.

Many thanks in advance

Jenie

1 ACCEPTED SOLUTION
Read only

hedvig_rohonyi
Product and Topic Expert
Product and Topic Expert
0 Likes
3,215

Hello Jenie,

1. counting the position specification for a charater begins with 0.
You can test this e.g.

write: wa_mara-matnr+1. -> 0-30412
write: wa_mara-matnr+0. -> 20-30412

online help
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenoffset_glosry.htm

2. Regarding Line of match: You are right. See please my second attachment.

3. Value is = -20. It is not negative, but you use command

wa_tab1+wa_tab1_result-offset(3) -> "20-" and the third chca-del-pngoffset-0.pngaracter is -.

Best regards,
Hedvig

Hallo experts,
I am trying to find a specific value 'material number' in an internal table , to do this I have wrote the code below , but it still missing me something, as I am receiving no results.

TYPES: BEGIN OF tab2,
lo_matnr TYPE matnr,
 END OF tab2.
DATA: BEGIN OF wa_tab1,
wa_matnr TYPE matnr,
 END OF wa_tab1,
 tbl_tab1 LIKE TABLE OF wa_tab1
                     WITH KEY wa_column1,
      tbl_tab1_result TYPE match_result_tab,
      wa_tab1_result LIKE LINE OF tbl_tab1_result.

wa_tab1-wa_matnr = wa_mara-matnr.
APPEND wa_tab1 TO tbl_tab1.
FIND ALL OCCURRENCES OF '20-30412' IN TABLE tbl_tab1
              RESULTS tbl_tab1_result IN CHARACTER MODE.
LOOP AT tbl_tab1_result INTO wa_tab1_result.
  READ TABLE tbl_tab1 INTO wa_tab1 INDEX wa_tab1_result-line.
  IF sy-subrc = 0.
    WRITE:/ 'Line:', wa_tab1_result-line, 'Offset:', wa_tab1_result-offset.
    WRITE: 'Value is:',  wa_tab1+wa_tab1_result-offset(3).
  ENDIF.
ENDLOOP.

Many thanks in advance

Jenie

8 REPLIES 8
Read only

DoanManhQuynh
Active Contributor
0 Likes
3,215

I think this may be the problem:

tbl_tab1 LIKE TABLE OF wa_tab1 WITHKEY wa_column1,

as the table must be standard table with no secondary table keys, why dont you try: (orelse your table dont have that data pattern)

TBL_TAB1 TYPE TABLE OF TAB2 WITH KEY LO_MATNR,
Read only

former_member539238
Participant
0 Likes
3,215

If you need only one entry try using 'READ' or if you need multiple entries and you need to loop then you can use 'LOOP AT WHERE'.

LOOP AT tbl_tab1 INTO wa_tab1 WHERE wa_matnr = '20-30412'.
*  Write the code here
ENDLOOP
Read only

hedvig_rohonyi
Product and Topic Expert
Product and Topic Expert
3,215

Hello Jenie,

see my attachment, with a little modification your code is working.

(do not use "-" in names in Unicode programs (WA_MARA-MATNR) or wa_MARA was an internal table?

Best regards,
Hedvig

Read only

0 Likes
3,215

Hallo Hedvig,

Thanks for your reply , but where can I find your attachment ?

Jenie

Read only

3,215

Hallo Jenie, I uploaded again, I hope you can see it now.

Best regards,

Hedvig

Read only

0 Likes
3,215

Thank you very much, really helpful.

I have some questions that i hope you can clarify them to me as I find the results somehow not realistic or maybe I am misunderstanding this opertion. So the result above showes :

Line of Match = 1 ' this mean for me that once i do to mara i will find the matnr 20-30412 in the first line and this is not case as 20-30412 will be found in the row 50250.

Offset from Beginning of Line = 0 ' here once LINE = 1 so the offset will be accordingly 0.

Value is = -20. I don't know why here the Value is negative.

Many thanks in advance .

Jenie

Read only

matt
Active Contributor
0 Likes
3,215

You should really try debugging before asking these kinds of questions.

Read only

hedvig_rohonyi
Product and Topic Expert
Product and Topic Expert
0 Likes
3,216

Hello Jenie,

1. counting the position specification for a charater begins with 0.
You can test this e.g.

write: wa_mara-matnr+1. -> 0-30412
write: wa_mara-matnr+0. -> 20-30412

online help
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenoffset_glosry.htm

2. Regarding Line of match: You are right. See please my second attachment.

3. Value is = -20. It is not negative, but you use command

wa_tab1+wa_tab1_result-offset(3) -> "20-" and the third chca-del-pngoffset-0.pngaracter is -.

Best regards,
Hedvig