On Thu, May 12, 2011 at 10:23:29AM +0100, Nathalie Conte wrote: <snip> > I have this file format > chr start end strand > x 12 24 1 <snip> > I have this script to split and iterate over each line, but I don't > know how to group 2 lines together, and take the start of the firt line > and the end on the second line? could you please advise? thanks
my $file = 'chrome.dat'; my %hash; open my $FH, '<', $file or die "Unable to open $file: $!, stopped "; while( my $line = <$FH> ) { my ( $chr, $start, $end, $strand ) = split /\s+/, $line; # on the assumption there might be a chrom. x, strand 2 my $key = $chr . ':' . $strand; if( exists $hash{$key} ) { $hash{$key}{'start'} = $start if( $start < $hash{$key}{'start'} ); $hash{$key}{'end'} = $end if( $hash{$key}{'end'} < $end ); } else { $hash{$key}{'start'} = $start; $hash{$key}{'end'} = $end; } } close $FH or die "Unable to close $file: $!, stopped "; my $outfile = 'chrome_out.dat'; open my $OFH, '>', $outfile or die "Unable to open $outfile: $!, stopped "; for my $k (sort keys %hash) { my ( $chr, $strand) = split /:/, $k; printf $OFH "%s\t%s\t%s\t%s\n", $chr, $hash{$k}{'start'}, $hash{$k}{'end'}, $strand; } close $OFH or die "Unable to close $outfile: $!, stopped "; HTH, Mike -- Satisfied user of Linux since 1997. O< ascii ribbon campaign - stop html mail - www.asciiribbon.org -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/