might be a difference in style but i would do it like this
on *:text:!change & &:#:{
  var %a = 1
  var %b = $lines(file.txt)
  while (%a <= %b) {
    var %line = $replace($read(file.txt,%a),$2,$3)
    .write -l $+ %a file.txt %line
    inc %a

seems to me that would only need 1 line 1 loop to replace all the matches (assuming data1 data2 data3 are all single words and distinctly different).
if data1 data3 both were the same (just for instance) and you wanted to change data3, these 2 scripts would change both data1 and data3 regardless, if they were the same value to start with.