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

In reply to Re: merge two files by Utilitarian
in thread merge two files by karey3341

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.