Perl : joining alternate lines

Hi,

I need to join every alternate line in a file

for eg:input file

$ cat abc
abc
def
ghi
jkl

output

abc def
ghi jkl

code i wrote for this

$ cat add_line.pl
#!/usr/bin/perl -w

my $count=1;
#my $line=undef;
my @mem_line;
my $i=0;
my $x=0;

while(<>)
{
chomp;
$x=$count % 2;
#print "$x $_\n";
        if ( $x == 1 )
        {
                $mem_line[$i] = "$_";
        }
        else
        {
                $mem_line[$i - 1] .= " $_\n";
        }
$count+=1;
$i+=1;
}

print "@mem_line";

I am gettin the desired output but with the following error

./add_line.pl abc
Use of uninitialized value in join or string at ./add_line.pl line 26, <> line 4.
abc def
ghi jkl

can you please help my understand this error

Thanks
Sam

Hi sam05121988,

Beacuse your are incrementing the i counter in every line. You should do it every two lines. Otherwise you will have empty slots in the array that print that warning message. Like:

while(<>)
{
chomp;
$x=$count % 2;
#print "$x $_\n";
        if ( $x == 1 ) 
        {   
                $mem_line[$i++] = "$_";
        }   
        else
        {   
                $mem_line[$i - 1] .= " $_\n";
        }   
$count+=1;
#$i+=1;
}

Not perl, but try:

awk '{getline l;print $0,l}' input