in reply to Seeking help with script

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.

Replies are listed 'Best First'.
Re^2: Seeking help with script
by docalf (Initiate) on Jun 29, 2010 at 15:30 UTC

    Thanks WW I'll go over your coments and Toolic's.

    I really appreciate the answers and the effort to teach me not only to solve my problem.

    Sincerely,
    docalf