Where you have
if ($sid eq $sidtwobinder[0])
you are not getting a match because
$sid is "1" and
$sidtwobinder[0] is "sid 1" (note the two spaces between sid and 1). You could change your
@sid array to contain the whole text. I don't know how big your dataset is but it is inefficient to loop through
@unpairedSidBinder inside the outer loop. You could split it into two stages, saving the matches in a hash of arrays - something like:
my @sid = ('sid 1','sid 2','sid 7','sid 9');
my %hoa;
foreach my $el (@unpairedSidBinder){
my ( $key, $data ) = split /\|/,$el;
push( @{ $hoa{$key} }, $data );
}
foreach my $key (@sid) {
my $ary = $hoa{$key} or next;
my $temp = join(" ", @$ary );
print "$key|$temp\n";
}