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: 

Compare 2 value in column of internal table.

hungnth2709
Explorer
0 Kudos
954

I have a question. I want compare value a[i] and a[i+1] in column Value 1 of internal table. If equal, set column Value 2 equal 0 (like picture).

Plz help me. Thanks very much.

8 REPLIES 8

KatiNonhebel
Advisor
Advisor
0 Kudos
871

Thank you for visiting SAP Community to get answers to your questions.

The more details you provide, the more likely it is that members will be able to respond.
I recommend that you familiarize yourself with our Q&A Tutorial: https://developers.sap.com/tutorials/community-qa.html, as it provides tips for preparing questions that draw responses from our members.
Should you wish, you can revise your question by selecting Actions, then Edit.By adding a picture to your profile you encourage readers to respond:https://www.youtube.com/watch?v=46bt1juWUUM

Many thanks!
Keep in mind, when you receive an answer that was helpful to you, accept it as best answer.

Good Luck

Kati - SAP Community Moderator

MateuszAdamus
Active Contributor
871

Hello hungnth2709,

Have you even tried to do it on your own?

You should use the LOOP, IF and READ TABLE with index statements to achieve your goal.


Kind regards,
Mateusz

joltdx
Active Contributor
871

For this simple scenario, how about simply doing the loop thing?

FIELD-SYMBOLS <previous_table_line> LIKE LINE OF internal_table.
LOOP AT internal_table ASSIGNING FIELD-SYMBOL(<table_line>).
  IF sy-tabix > 1 AND
    <table_line>-value1 = <previous_table_line>-value1.
    CLEAR <table_line>-value2.
  ENDIF.
  ASSIGN <table_line> TO <previous_table_line>.
ENDLOOP.

Loop through all lines and keep the "previous" line. Starting at index 2, compare the current line to the previous line and act as needed... As a basic programming exercise, this is one solution.

But if this is not your entire scenario, if there is more to it, there might be better solutions. How do you get the data to begin with? Can something be done there? What are you going to do with the data later on? Do you really need the duplicate entries? And so on...

871

Hi jorgen_lindqvist41

Never seen this:

  ASSIGN <table_line> TO <previous_table_line>.<br>

If you change the assignment to <table_line> it does not impact <previous_table_line> ??

joltdx
Active Contributor
0 Kudos
871

Actually this works just fine, but I agree it's not really obvious at first sight. 🙂 And I don't think I've ever really used it in real life.

But in the ASSIGN statement, it is the memory are represented by <table_line> that is assigned to <previous_table_line>. So at that moment, both <table_line> and <previous_table_line> refers to the same memory area. So if the next statement were to change <table_line>-value1, then that would of course also be reflected in <previous_table_line>-value1. Obviously.

Then, at the next LOOP pass, that assign is changing which memory are <table_line> is refering to, but it's not changing <previous_table_line>, so that one is still refering to the same memory area as it was before.

Here's my entire example if you would like to try it out without typing it all... 🙂

TYPES:
  BEGIN OF ty_structure,
    value1 TYPE string,
    value2 TYPE i,
  END OF ty_structure.
TYPES ty_structure_tt TYPE STANDARD TABLE OF ty_structure WITH EMPTY KEY.

DATA(internal_table) = VALUE ty_structure_tt( ( value1 = 'a1' value2 = 10 )
                                              ( value1 = 'a1' value2 = 10 )
                                              ( value1 = 'a1' value2 = 10 )
                                              ( value1 = 'a2' value2 = 12 )
                                              ( value1 = 'a2' value2 = 12 )
                                              ( value1 = 'a3' value2 = 13 )
                                              ( value1 = 'a3' value2 = 13 ) ).

FIELD-SYMBOLS <previous_table_line> LIKE LINE OF internal_table.
LOOP AT internal_table ASSIGNING FIELD-SYMBOL(<table_line>).
  IF sy-tabix > 1 AND
    <table_line>-value1 = <previous_table_line>-value1.
    CLEAR <table_line>-value2.
  ENDIF.
  ASSIGN <table_line> TO <previous_table_line>.
ENDLOOP.

out->write( internal_table ).

0 Kudos
871

I always considere Field-Symbol as CharPointer (like in C). So for me the field-symbol contains the adresse of the memory.

And <previous_table_line> = "Adresse of <table_line"

Second surprise of the year 🙂

joltdx
Active Contributor
0 Kudos
871

I think the same way, and the key here is the ASSIGN statement.

ASSIGN <table_line> TO <previous_table_line>.

ASSIGN is working with the addresses and will make <previous_table_line> contain the address to whatever <table_line> is addressing. But

<table_line> = <previous_table_line>.

will work on the "content of the memory", and change the data that <table_line> is addressing to the data that <previous_table_line> is addressing...

ABAP is awesome! 🙂

0 Kudos
871

thanks u very much.