On Feb 11, 2004, at 2:55 PM, James Edward Gray II wrote:

[snip]

my @char = ( /[a-z]/ig, ( '-' ) x $len )[ 0 .. $len - 1 ];

If I may, yuck! This builds up a list of all the A-Za-z characters in the string, adds a boat load of extra - characters, trims the whole list to the length you want and stuffs all that inside @char. It's also receives a rank of "awful", on the James Gray Scale of Readability. ;)

[snip]


Ok, now I understand. I found that my problem was with how the "next" command was operating in conjunction with the grouping of characters. Ok, making progress. :-)

Now, about that array slice I have:

my @char = ( /[a-z]/ig, ( '-' ) x $len) [0 .. $len - 1];

I know it wastes a lot of memory and makes perl do much extra work. However, when I try to replace that line with something like this:

my @char = ( /[a-z]/ig, ( '-' ) x ($len - length) ;

it doesn't work the way I thought it would (gee what a thought). I would like to express the code similar to
( '-' ) x ($len - length)
because it is easy for me to read and it tells you clearly what is going on. However, every time I try to implement something like that I get unexpected output or I have to really rewrite the loop. Which I have been unable to troubleshot as you have been seeing. :-) I think the 'length' command it also counting any '\n' characters or something, because my out put ends up with different lengths like this when I use the ($len - length) way :


 a c u g a c g a g u - - - - - - - -       bob
 a c u g a c u a g c u g - - - - - - -       fred

with this input:

>bob
actgacgagt

>fred
actgactagctg


The reason I went with /[a-z]/ig is because some sequence data uses other letters to denote ambiguity and other things. I guess I can only list the letters it uses but I was just lazy and typed in the entire range of "a to z".


I will be continuing to work on it but here is the code as it stands now (with that awful array slice).

#!/usr/bin/perl

use warnings;
use strict;

print "Enter the path of the INFILE to be processed:\n";

# For example "rotifer.txt" or "../Desktop/Folder/rotifer.txt"

chomp (my $infile = <STDIN>);

open(INFILE, $infile)
                or die "Can't open INFILE for input: $!";

print "Enter in the path of the OUTFILE:\n";

# For example "rotifer_out.txt" or "../Desktop/Folder/rotifer_out.txt"

chomp (my $outfile = <STDIN>);

open(OUTFILE, ">$outfile")
                or die "Can't open OUTFILE for input: $!";

print "Enter in the LENGTH you want the sequence to be:\n";
my ( $len ) = <STDIN> =~ /(\d+)/ or die "Invalid length parameter";


print OUTFILE "R 1 $len\n\n\n\n"; # The top of the file is supposed


$/ = '>'; # Set input operator

while ( <INFILE> ) {
chomp;
next unless s/^\s*(.+)//; # delete name and place in memory
my $name = $1; # what ever in memory saved as $name
my @char = ( /[a-z]/ig, ( '-' ) x $len) [0 .. $len -1]; # take only sequence letters and
# and add '-' to the end
my $sequence = join( ' ', @char); # turn into scalar
$sequence =~ tr/Tt/Uu/; # convert T's to U's
print OUTFILE " $sequence $name\n";
}



close INFILE; close OUTFILE;


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to