[Pdx-pm] read and concatenate two lines at a time

Ken Brush ken at cgi101.com
Fri Dec 3 22:32:11 CST 2004


On Friday 03 December 2004 16:05, Thomas J Keller wrote:
> Greetings,
> I need to concatenate every two consecutive lines from a large data
> file. Is there an easy way to do this?
>
> Below is the snippet of what I tried, but it gives errors sometimes -
> if anyone can point out why it works for some lines and not for others,
> I'd appreciate that too.
> Thanks,
> Tom
> ########################
> while (<>){
> 	chomp;
> 	my $line = $_;
> 	$line =~ s/\t\./\t1/g;				## substitute "1" for "." values
> 	if ( $line =~ /^\w\w\d\d\d\d/) {
> 		push @evens, $line;
> 	} else {
> 		push @odds, $line;
> 	}
> }
>
> foreach (0 .. $#evens) {
> 	push @unsorted, $evens[$_].$odds[$_];
> }
>

You could just do:

while(<>) {
	chomp;
	$line = $_ . <>;
	chomp($line);
	push(@unsorted, $line);
}

-Ken


More information about the Pdx-pm-list mailing list