open( SEQ, ">sequential_output.file" ); $byteoffset = 0; while there's data to be written { $breakpoint{$byteoffset} = "" if this location might need to get an insertion at a later point if I have sequential data { print SEQ $sequential_data; $byte_offset += length($sequential_data); } else { # this is data that needs to be back-fitted to # a byteoffset that I stored earlier $back_offset = whatever previous byteoffset is right $breakpoint{$back_offset} .= $backfit_data; } } close SEQ; $breakpoint{$byteoffset} = ""; # do that so the following loop will handle the final chunk # final loop: put all the pieces together # in the intended order open(SEQ, "final_output.file"); foreach $chunk ( sort {$a<=>$b} keys %breakpoint ) { sysread( SEQ, $chunkdata, $chunk ); print OUT $chunkdata, $breakpoint{$chunk} }