Compare current row's data with the previous row?


How can I create a function which reads the data from the file and compare the current row with the previous row to check if the data has changed or not? if the data in the current row is same as the previous one then save that data with ID-1 but if the next record is different than the previous record then save a new record with ID-2?

What do you mean by “save”? Do you want to manipulate the file or do you want to write to a second file?

I want to right to a second file


Another question: What do you want to happen if three consequtive rows are the same? How many and which rows shall be written to the second file?

I just want to accumulate the amounts which are in those three rows. For example, I have three fields and if the record number is same as the previous record then I want to accumulate the numbers and write a record into the file


Is it possible that the same record number can appear on multiple rows that are not adjacent? Is this possible?

foo 123
bar 323
foo 456
bar 434
foo 987

For an input like this, there would be two record numbers ( foo and bar).

Yes, this can happen as well

Then you will have to do something like this:

  • create a map with record numbers as keys and arrays amounts as values
  • iterate over the rows of the input
    • extract the record number and the amount from the row
    • look up the record numer in the map
      • if it exists, append the amount to the array of amounts
      • if it does not exists, create a new array, append the amount to it and store the array as the value for the record number in the map
  • iterate over the keys (= record numbers) of the map
    • write a record containing the key and the corresponding array of amounts to the output
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.