The first problem I see is that you increment "$count" as soon as you put the first line of each entry into each of your arrays. Then when you hit the second line of each entry, $count is putting the long string of letters into the next element of your "pdbString" array. So when you go to print to your output file(s), you are getting the wrong pdbString data for each entry (and this string is empty/undefined for the first entry).

(Update: Forgot to mention: because of the way you used your $count variable, all the "pdbString" values end up as empty strings: you increment $count before you store the string in that array, then when you hit the start of the next entry, you assign "" (empty string) to that same array element.)

Here's a cleaned up version of your code, with "use strict" included (because that's what all good monks do), and a fair bit of seemingly unnecessary stuff left out.

#!/usr/bin/perl -w use strict; my ( @PDBchain, @PDBcode, @pdbString, @SCOPclass ); open (IN, $ARGV[0] ); while (<IN>) { chomp; if ( /^>d(\d\w{3})(\w[_\d])\s(\w)/ ) { push @PDBcode, $1; #SAVE PDB CODE push @PDBchain, $2; #SAVE PDB CHAIN push @SCOPclass, $3; #SAVE SCOP CLASS (A,B,C,D,E,F,G) push @pdbString, ""; } else { $pdbString[$#pdbString] .= $_; } } close IN; print "Total number of entries: ".scalar( @PDBcode ). "\n"; #COUNT TOT +AL NUMBER OF PDBS REPRESENTED for ( 0 .. $#PDBcode ) { open (OUT, ">>$PDBcode[$_]_$PDBchain[$_].fa"); print OUT ">",$PDBcode[$_], "_", $PDBchain[$_], "\n"; print OUT $pdbString[$_], "\n"; close OUT; }
Given the input you showed, that produces two output files, with one entry in each file. BTW, the OP code saved stuff into a "@SCOPclass" array, but then never used it. Was that intentional?

Oh, another thing about the OP code: the last two print statements of the while loop are never reached. That's okay, you don't need them anyway, but proper indentation of your code might help to avoid misunderstandings.


In reply to Re: Problems with Variable Scope in Perl by graff
in thread Problems with Variable Scope in Perl by InfoSeeker

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.