You are almost there :-) my ($helper1, $helper2); my $counter = 1; foreach my $line(@list){ chomp $line; my @coordinates = split(/' '/, $region); my $chromosome = $coordinates[0]; my $start = $coordinates[1]; my $end = $coordinates[2]; my $strand = $coordinates[3]; # Using a simple modulo operation (returns 1 if counter is an uneven number and 0 otherwise # you can simply decide the even and uneven lines on the uneven line you capture the Chromosome and Start # and on the even lines you capture the End and the Strand, as well as printing out the result of the # beginning of the previous line and the end of the current line. # # Using a for loop instead of a for each loop will result in a nicer looking loop # and it might (never actually tested this be a little bit faster as well (benchmark that to be sure) # which on large amounts of data as you are likely to be processing might save you a # decent bit of time. if ( ! $counter % 2 ) { $helper1 = "$chromosome $start"; } if ( $counter % 2 ) { $helper2 = "$end $strand"; print "$helper1 $helper2\n"; } $counter++; }
Hope that helps, Rob On Thu, May 12, 2011 at 11:23 AM, Nathalie Conte <n...@sanger.ac.uk> wrote: > > HI, > > I have this file format > chr start end strand > x 12 24 1 > x 24 48 1 > 1 100 124 -1 > 1 124 148 -1 > > Basically I would like to create a new file by grouping the start of the > first line (12) with the end of the second line (48) and so on > the output should look like this: > x 12 48 1 > 1 100 148 -1 > > 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 > > unless (open(FH, $file)){ > print "Cannot open file \"$file\"\n\n"; > } > > my @list = <FH>; > close FH; > > open(OUTFILE, ">grouped.txt"); > > > foreach my $line(@list){ > chomp $line; > my @coordinates = split(/' '/, $region); > my $chromosome = $coordinates[0]; > my $start = $coordinates[1]; > my $end = $coordinates[2]; > my $strand = $coordinates[3]; > ...??? > > > > -- > The Wellcome Trust Sanger Institute is operated by Genome Research Limited, > a charity registered in England with number 1021457 and a company registered > in England with number 2742969, whose registered office is 215 Euston Road, > London, NW1 2BE. > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > >