in reply to subroutine problem

You have several problems with your use of the @pairs array, first you are assigning to it using @pairs = "$segment1[$i-1]$segment1[$i]\n";, which ensures that the array will never contain more than one entry, you probably want push(@pairs, $segment1[$i-1].$segment1[$i]) instead. Immediately after that assignment however, you are assigning it again with @pairs = split('', $nn_pair), so the content from the previous assignment is useless because you just overwrote it with whatever was split from $nn_pair (which you never assigned, and thus @pairs is always empty after this line). Perhaps your first assignment to @pairs should have been assigning to $nn_pair instead?. (use strict; will help you find these kinds of problems)