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. :-)
Excellent. I knew we would get there.
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:
Ah, it's pretty small potatoes to quibble over, really. I don't think it's in any danger of slowing your code significantly or making you buy more RAM.
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)
Na, something like this won't work because you won't know the length of those characters, until you stick the somewhere. Length by default works on $_, which still contains a big mess of sequence characters and whitespace.
I think your big hang up is trying to do it all in one line. Two or three is fine, right? <laughs> And of course, there's nothing wrong with the current solution. It does work. You only need to replace it if you want to. There's always more than one way. Use what you like.
I'll see if I can add a suggestion below...
#!/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
Right here, $_ holds our sequence, plus some junk. We can just work with $_ then, if we want to.
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
Alternative to the above two lines:
tr/A-Za-z//cd; # remove junk from $_ $_ .= '-' x ($len - length) if length() < $len; # add dashes s/\b|\B/ /g; # space out
$sequence =~ tr/Tt/Uu/; # convert T's to U's
Then this would become:
tr/Tt/Uu/;
print OUTFILE " $sequence $name\n";
And this:
print OUTFILE "$_ $name\n";
}
close INFILE; close OUTFILE;
Hope that helps.
James
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>