in reply to merge two files

Update, should take my own advice, misread your intention entirely. I didn't realise the "y co-ordinate" needed to be summed

Look at the structure of your data. You want to sort a list of numbers where each list is associated with a unique key What do you think is the data structure you need to put the data into?
Something like:
%records{$number_before_colon => @list_of_numbers_seperated_by_commas}
So read each file splitting the result into an index and an array and add the array to a hash on the index.

while(<INFILE>){ chomp; ($index,@record)=split/[:,]/; push @{$records{$index}},@record; }
When you need to do data conversion, half the battle is examining the relationship between the data structures. Now you can access the data in a sorted order:
for $index (sort{$a<=>$b}(keys (%records))){ print $OUTFILE "$index: ",join(",",(sort{$a<=>$b}(@{$records{$ +index}}))),"\n"; }
Wrapping it up
#!/usr/bin/perl use strict; use warnings; my @files=qw(temp.data temp1.data temp2.data); my ($file,$index, %records,$INFILE,$OUTFILE); for $file (@files){ open($INFILE,"<","$file")|| die "Failed to open $file: $!"; while(<$INFILE>){ my @record; chomp; ($index,@record)=split/[:,]/; push @{$records{$index}},@record; } close $INFILE; } open ($OUTFILE,">","newfile.data")|| die "Failed to open newfile.data: + $!"; for $index (sort{$a<=>$b}(keys (%records))){ print $OUTFILE "$index: ",join(",",(sort{$a<=>$b}(@{$records{$ +index}}))),"\n"; } close $OUTFILE