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.


In reply to Re^3: foreach loop by Gulliver
in thread foreach loop by morio56

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.