You and your friend came close. Most of your problems, including the uninit issues, were the result of 'off-by-one' errors in array assignment, etc.

That said, this is also close (only "close" because the output formatting is NOT per your spec; correction is left as an exercise...) and utilizes what I believe to be some relatively minor corrections to your code.

#!/usr/bin/perl use strict; use warnings; # 847021 =head OUTPUT OF THIS VERS: >Long_DNA_Scaffold.0 atgctagsctgtagctagctagtcgatgctagtagct ttagctagctgatgctagtcgatgctagtcatctagc >Long_DNA_Scaffold.1 tagctagctagctgatcgtagctagtcgtagctagct ggtcgatgctgatcgtagctgatgctagtcgatgcta > # removal of ">" and blank lin +es left as an exercise.... >Long_DNA_Scaffold2.0 atgctagsctgtagctagctagtcgatgctagtagct tagctagctgatcgtagctagctgatgctagctagtc >Long_DNA_Scaffold2.1 tagctagctagctgatgctagctagtcgatcgtagct ggctagtgctagtgctagctgatgctagtcgtagctg =cut my $file = "847021.txt"; open (IN, '<', $file) or die "Can't open $file: $!"; # test your fi +le opens! $/=">"; my $i=0; my $separator_count=0; my (@seq, $seq, @name, $name); while (<IN>) { if(/^(\S+)?.*?\n(.*)/ms) { # $i++; # this creates + an empty ("undef") element 0 in the arrays; mis-located; moved $seq[$i]=$2; $name[$i]=$1; } else { $separator_count++; if ($separator_count > 1) { print "No match for current data: $_\n\n"; # one inst +ance is expected because the data begins with record separator; don't + show it } else { next; # create no u +ndef $arr[0] } } $i++; # re-located +from line 57 } close IN; print "\n"; for my $j(0..$#seq) { # $#seq: in +dex of last element. 0..$i replacing OP's 1..$i $name[$j] =~ tr/>//; # replaces OP +'s mis-coded tr//; see toolic's reply, above re tr syntax $seq[$j] =~ tr/>//; my @sequence=split(/N+/,$seq[$j]); my $n=length(@sequence); my$seqs_to_print=$n; # OP's decrem +ent caused failure to print second elements of @seq for my $k(0..$seqs_to_print){ # use $i, $j, + $k for counters; $x and company are typically for substantive data print ">$name[$j].$k\n"; print "$sequence[$k]\n"; } $j++; }

Those comments above re good practice should be read as ADDITIONS to toolic's list.


In reply to Re: Seeking help with script by ww
in thread Seeking help with script by docalf

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.