Application Development 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: 

How to compare range with range?

britta_leib2
Participant
0 Kudos

Dear ABAP friends,

I have an itab A (structure BAPI6109SEL) at runtime and I have a Z-table B with the same structure.

Now I want to check if the value of any line in the itab A is in the content of Z-table B (like “if A is in B”).

E.g. 

- I EQ 123 <-> I BT 124 129 -> FALSE

- I CP ABC* <-> I BT ABC000 ABC999 -> RIGHT

- I CP 506* <-> I BT 5066000 5069ZZZ -> FALSE

I know that I can compare each field of the structure and replace the CP (*) by an interval 000 (low) and 999/ZZZ (high). But this is very expensive coding.

Is there any clue to do it in an easier way!

Thanks in advance!

Britta

4 REPLIES 4

rdiger_plantiko2
Active Contributor
0 Kudos

Are both A and B allowed to contain arbitrary signs and options?

Can any of the following expressions appear in A and in B?

I LT 100

E BT 049 099

I GE 089

E BT 104222 105672

E EQ 102AZ7

E CP 17++A5

Is this your question: You want to find out whether the following holds:

Does every x satisfying "x in A" also satisfy "x in B" ?

Or do you want to find even more:

Does every x satisfying "x in A" also satisfy "x in B" and vice versa?

Anyway, I think an algorithm that handles all the above operators will get very complicated. (Since BAPI6109SEL-LOW is of type CHAR45, checking all possible x is definitely no option!).

Regards,

Rüdiger

0 Kudos

Hello Rüdiger,

there are only allowed the follwing signs and options:

I EQ, I BT and I CP. * is allowed, but + is not allowed.

The data from itab A comes from an InfoPackage selection (data selection tab) in module BW. The BAPI6109SEL-LOW type (CHAR45) is so long, since it has to be variable.

E.g. fieldname=PRCTR (profit center) has type CHAR10, another fieldname=FICTR (funds center) has type CHAR16.

In Z-Table B we have saved the allowed values.

Therefore we want to check the values from itab A versus the values in Z-table B.

Regards, Britta

0 Kudos

Britta,

as it is clearly no option to check every single value, I would suggest to solve the problem as follows:

Transform both range tables into a canonical form, and then compare these forms.

To get such a form, it is helpful to know that in your case there are only including, no excluding signs, so a range describes a set-theroretical union of each of its single lines.

What do I mean be canonical form?

There are many range tables which actually describe the same set of values. For example,

I BT 1 2

I BT 3 4

is different from

I BT 1 3

I BT 2 4

but they both describe the same set of values. This defines an equivalence relation on the set of all range tables:

    Two range tables A and B are equivalent if for every x: "x in A" holds if and only if "x in B" holds.

An equivalence relation decomposes a set into a disjoint union of subsets, called equivalence classes (it's easy to check that this relation indeed is an equivalence relation, being reflexive, symmetric and transitive).

A canonical form would be a function (here: an algorithm) selecting one particular representant for each of these subsets.

If you have an algorithm to transform a range table into a canonical form, then you can transform two range tables A and B into their canonical forms ^A and ^B. You then know: A and B will describe the same set of values if and only if ^A = ^B.

Let's start with a simple case: Suppose there would be only 'I EQ' values in the range. Then a canonical form would be achieved by sorting the range table by the LOW value and removing duplicate entries.

If you transform both A and B into their canonical form, it will be easy to compare them. If, sticking still to the simple case, both ranges consist only of single values:

  • They will then and only then describe the same set of values if their canonical forms are equal as internal tables.
  • There will be values contained in the set described by A but not in B if and only if the canonical form of A contains an entry which is not contained in B.
  • The set of values x satisfying "x in A" will be a subset of those x satisfying "x in B" if and only if all the entries of A's canonical form are also contained in B's canonical form.

Next stage: What could be a canonical form for ranges containig "I EQ" and "I BT" entries?

Sorting by LOW is still a good idea. But ranges should be simplified, otherwise ranges describing the same set of values would not give the same canonical form:

  • Single values contained in an interval should be eliminated: "I BT 1 3" and "I EQ 2" should be comprised  to "I BT 1 3".
    Formally: Make an auxiliary range table A' containing all BT selections. Then loop over all single values of the range and delete them if they are in A' (here in means the ABAP IN operator).
  • Replace overlapping intervals by their union interval: "I BT 1 3" and "I BT 2 4" should be comprised to "I BT 1 4".
    Formally: Still working with the auxiliary range A' of all BT lines:  Loop over A'. Look ahead in the subsequent entries of the current loop entry to find overlaps: The next LOW value is contained in the interval (LOW,HIGH) of the current loop entry. Then replace the current (LOW,HIGH) by (LOW, HIGH*) with HIGH* being the maximum of the two HIGH values of the overlapping BT entries.

If you have implemented the strategy to obtain the canonical form, you can apply that strategy to A and B and then comfortably compare them. They are equal if and only their canonical forms are equal. By comparing the single lines of the canonical forms, you can find out whether there are

  • values x satisfying "x in A" but not "x in B"
  • values x satisfying "x in A" and "x in B"
  • values x satisfying "x in B" and not "x in A"

(the fourth set will probably always be the case: There will always be values contained neither in A nor in B. But this is not interesting.)

Since I am not on your company's payroll, I will abstain from providing you the complete implementation of such an algorithm. I better stop here and care for the problems of my company...

Regards,

Rüdiger

Former Member
0 Kudos

hello,

If the itab A and Itab B have some key fields and if you need to check on those fields. A simple way would be

Loop at itab A and do read on itab B (contains all data from Z table) with the key fields. This way you can find if a line of itab a is in itab B.

best regards,

swanand