in reply to Problems with Variable Scope in Perl
(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.
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?#!/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; }
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problems with Variable Scope in Perl
by Anonymous Monk on Nov 03, 2008 at 01:12 UTC | |
by ruzam (Curate) on Nov 03, 2008 at 02:34 UTC | |
by ig (Vicar) on Nov 03, 2008 at 02:16 UTC | |
by graff (Chancellor) on Nov 03, 2008 at 03:20 UTC | |
by Anonymous Monk on Nov 05, 2008 at 15:53 UTC |