Here's a version of Matija Nalis's script which uses the pairwise function from the List:MoreUtils module to join the two arrays, and also doesn't require that lines with matching IDs (first field) be on consecutive lines. i.e. they can be separated by any number of lines.
#! /usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw(pairwise);
sub parseline { my ($id,$v) = split; return $id, split //,$v };
my %ID=();
while (<>) {
my ($id, @line) = parseline();
if ( !defined($ID{$id}) ) {
push @{ $ID{$id} }, @line ;
} else {
my @paired = pairwise { "$a $b" } @{ $ID{$id} }, @line;
print join("\t", $id, @paired), "\n";
delete $ID{$id};
};
};
Rather than use a global variable for $id, MN's parseline subroutine has been modified to return both the id and an array of elements in each line.
$id is used as the key to a hash %ID for storing each parsed line. The first time we see a given $id, we just store the parsed line array (@line) in the hash and move on to the next line. The next time we see it, we pairwise join the stored array with the current @line array, print it out with TAB field separators, and then delete that $id from the %ID hash.
See man List::MoreUtils for details on how the pairwise function works. BTW, List::Util and List::MoreUtils are two excellent modules for doing all sorts of list (aka array) manipulations.
Output:
$ ./zara.pl zara.txt
1 1 2 1 1 1 1 2 2 2 1
2 2 1 2 1 2 1 2 2 1 2
3 2 1 1 1 1 1 2 2 1 2