Compare fields and synchronize values

Hi!

i have:

field1: A B C D E

sum1: 3 6 4 2 7

field2: C D

sum2: 3 1

I would like to have a result:

field1: A B C D E

sum1-sum2: 3-0 6-0 4-3 2-1 7-0

I have tried to create the cycle, but always i have the same result (probably, cycle makes 2 laps?):

field1: A B C D E

sum1-sum2: 3-3 6-1 4-3 2-1 7-0

With the code …@Contains(@Subset…) i have right result, when i see

field1: A B C D E

field2: C D

result1: 0 0 1 1 0

I was thinking, tht i need to create the cycle inside first cycle (because field2 and sum2 are shorter, than field1 and sum1. So i need to synchronize field1, result1 and sum2…

And i cant resolv the problem. :frowning:

Subject: Compare fields and synchronize values

@for( i := 1; i <= @Length(field1); i := i + 1;

. . . . match := @False;

. . . . @for( j := 1; j <= @Length(field2); j := j + 1;

. . . . . . . . @if( field1[i] = field2[j]; @Do( ans := sum1[i] - sum2[j]; match := @True) ; "')

. . . . );

. . . . result := result : @if( match; ans; sum1[i])

);

@for( j := 1; j <= @Length(field2); j := j + 1;

. . . . match := @False;

. . . . @for( i := 1; i <= @Length(field1); i := i + 1;

. . . . . . . . @if( field2[j] = field1[i]; @do(match := @True) ; “”)

. . . . );

. . . . @if( match; “”; result := result : -1*sum2[j] )

);

3 6 1 1 7 -3

but doesn’t work if the first list has gaps.

Ignore the following - I see I mis-understood the original problem thinking you wanted to add the values. I realized after writing the following I should just be able to add up the numbers and then I realized I misunderstood your question.

Are field 1 and 2 arrays of values?

Will values in 2 always be in the first array? Or could I have a Z in the second array that isn’t in the first array?

Are the values unique?

Assuming the above how about:

ans := 0;

@for( i := 1; i <= @Length(field1); i := i + 1;

. . . . match := @False;

. . . . @for( j := 1; j <= @Length(field2); j := j + 1;

. . . . . . . . @if( field1[i] = field2[j]; @do(ans := ans + sum1[i] - sum2[j]; match := @True) ; “”)

. . . . );

. . . . ans := ans + @if( match; 0; sum1[i])

);

@for( j := 1; j <= @Length(field2); j := j + 1;

. . . . match := @False;

. . . . @for( i := 1; i <= @Length(field1); i := i + 1;

. . . . . . . . @if( field2[j] = field1[i]; @do(match := @True) ; “”)

. . . . );

. . . . ans := ans - @if( match; 0; sum2[j])

);

field1: A B C D E

sum1: 3 6 4 2 7

field2: C D Z

sum2: 3 1 3

I think this would give the answer of 15

Subject: RE: Compare fields and synchronize values

in practice, i wanna:

field1: A B C D E

field2: C D

SUM2 70 1

FIELD_TOGETHER: 0 0 70 1 0

Where i have coincidences, i wanna these values in the right places in the array…

Subject: RE: Compare fields and synchronize values

Your original example showed minus the two values, so this is what you want?

Field1

A

B

C

D

E

Sum1

3

6

4

2

7

Field2

C

D

Sum2

3

1

Field_together

3

6

1

1

7

My question / concern is what happens is Field2 has values not in Field1?

(Maybe this is not possible given your data/configuration)

My code will definately not work as it doesn’t have the smarts to handle keeping the order.

Is this what you’d want?

Field1

A

B

C

S

Sum1

3

6

4

2

Field2

C

R

Z

Sum2

3

1

-2

Field_together

3

6

1

-1

2

2

Unfortunately my code would give you

Field1

A

B

C

S

Sum1

3

6

4

Field2

C

R

Z

Sum2

3

-2

Field_together

3

6

1

2

-1

2

Subject: RE: Compare fields and synchronize values

i wanna result, as u made in the first table!In practice, to make an array so long as field1! And to have the same numbers, as in the last string of that table! U have understood right!

Subject: RE: Compare fields and synchronize values

Then I think this should work (I haven’t type it in to test it so there maybe some basic syntax errors) by this give the the algorithm on how to approach it:

@for( i := 1; i <= @Length(field1); i := i + 1;

. . . . match := @False;

. . . . @for( j := 1; j <= @Length(field2); j := j + 1;

. . . . . . . . @if( field1[i] = field2[j]; @Do( ans := sum1[i] - sum2[j]; match := @True) ; "')

. . . . );

. . . . result := result : @if( match; ans; sum1[i])

);

Loop through each value in the first array and find if there is a corresponding value in the second array. If so update the difference and write it out, otherwise write out the original value.

This would also have a problem if either array had “duplicate” values.