Aren't the printf placeholders for the 3 columns of data?
I'm not sure how to fix the formula. I don't know how to iterate very well, at all. Any suggestions?
There are five items of data in the list passed to sprintf, not three. They are:
$data[$i][0]
$data[$j][0]
$data[$i][2]
$data[$j][2]
distance(\@coords_i, \@coords_j)
Therefore, your format string in the sprintf call should have enough placeholders (five!) to match this list and not merely the three currently present.