On Feb 11, 2004, at 1:27 PM, Michael S. Robeson II wrote:

[snip]

Anyway, though it works great I am having a tough time trying to figure out WHY it works.

See comments below, in the code.


[snip]

I think if I can understand the mechanics behind this script it will only help me my future understanding of writing PERL scripts.

Perl. The language you are learning is called Perl, not PERL. :)


[snip]

The working script:
_________

#!/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.


$/ = '>'; # Set input operator

Here's most of the magic. This sets Perl's input separator to a > character. That means that <INFILE> won't return a sequence of characters ending in a \n like it usually does, but a sequence of characters ending in a >. It basically jumps name to name, in other words.


while ( <INFILE> ) {
    chomp;

chomp() will remove the trailing >.


    next unless s/^\s*(\S+)//;
    my $name = $1;

Well, if we're reading name to name, the thing right a the beginning of our sequence is going to be a name, right? The above removes the name, and saves it for later use.


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. ;)


my $sequence = join( ' ', @char);

join() the sequence on spaces.


$sequence =~ tr/Tt/Uu/;

Convert formats.


print OUTFILE " $sequence $name\n";

Send it out.


}


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>




Reply via email to