in reply to Re^2: foreach loop
in thread foreach loop

This uses ikegami's approach; this will print the previous $chro as soon as a new '@' is found. You seem to want to print $sequence at the same time. This moves the print statements inside the loop. Also, you need to reset sequence after the new @ is found. If you would give us something more detailed than 'nnnn' we could see where things should go. Try using __DATA__ next time to hold some sample data for everyone to see.

use warnings; use strict; print "Using __DATA__ sample cns file:\n"; #chomp($t = <STDIN>); #open(THE, $t) or die "Can not open file: $!\n"; #@data = <THE>; my @data = <DATA>; #close THE; my $sequence=""; my $chro; foreach my $line (@data) { if ($line =~ /^\@/) { print $chro if length $chro; $chro = $line; print $sequence; $sequence = ""; } elsif ($line =~ /^[a-zA-Z]/) { $sequence .= $line; } else { print "no match: \$line=$line\n" } } #prints the last one, I'm sure theres a better way print $chro if length $chro; print $sequence,"\n"; __DATA__ @chr1 ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCGCTGCCCTGCC CCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGGCAGGAATAAGGAAAAGCAGC @chr2 CTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCCAGGCCAGTGCCGGGCCCCTCATAGGAGAGG AAGCTCGGGAGGTGGCCAGGCGGCAGGAAGGCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCC @chr55 CTGCAGGAACTTCTTCTGGAAGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAG

Update: would be better to replace the foreach with while (my $line=<DATA>) and get rid of @data. Especially since the files will probably be large.

Replies are listed 'Best First'.
Re^4: foreach loop
by morio56 (Initiate) on May 04, 2011 at 19:27 UTC
    Thanks. This seem to work with the print. It appears to me though that the print is done after each iteration. Is there a way to retain the values of @chr and $sequence for each iteration so that they can be accessed outside the foreach loop? The data am dealing with is actually nnnnnnnnn i.e an fq sequence file and what am showing here is just part of it.
    _DATA_ @chr1 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn @chr2 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn @chr4 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn @chr5 nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
      Of course there is!

      This is Perl.

      However, to learn how to do so (we are here, after all, to help you learn, not to do it for you), I would suggest push. Using the example there, you could create an array with elements 0, 2, 4 ... containing the "@char\d" and the odd-numbered elements with the data. Alternately (and perhaps better), see the Tutorials on hashes, here in the Monastery
          ...just for starters, that is.