Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to use this subroutine

by wjw (Priest)
on Oct 16, 2017 at 15:55 UTC ( [id://1201462]=note: print w/replies, xml ) Need Help??


in reply to How to use this subroutine

I modified your script just a bit to show you what I believe it is supposed to do. If you are just trying to get this done, what is below should work for you. I name the sequence files in the script instead of using ARGV, and otherwise just made changes to make it work.

It is still not pretty, but it will run as re-written. I would suggest you look into the perlref tutorial to see why things did not work in the codes as you posted it.

If you are really interested in why the code did not work for you as hoped, note each change made, then take both your original code and the little hack below and run them with perl -d. If the Perl debugger is too much for you (it should not be, it is pretty simple) Put print statements in various places that are useful to you so you can track what is happening in that subroutine. Better yet, do both.

#!/usr/bin/perl use warnings; use strict; my $sfile_1 = "f0.txt"; my $sfile_2 = "f1.txt"; my ($id1, $seq1) = read_fasta($sfile_1); my ($id2, $seq2) = read_fasta($sfile_2); my $colno = 12; pretty_print($seq1, $colno); pretty_print($seq2, $colno); sub read_fasta { ## reads a single sequence from a fasta file my $seqFile = shift @_; my $seq = ""; my $id = ""; open(my $in, "<", $seqFile) or die "unable to open $seqFile $!\n"; while(<$in>){ chomp; if($_ =~ /^>(\S+)/ ){ last if(length($id)); $id = $1; next; } if(length($id)){ $seq .= $_; } } return ($id, $seq); } sub pretty_print { my($seq, $colno) = @_; # We want to start from 0, and increment the starting position. # for this we use a classical loop. for (my $b=0; $b < length($seq); $b += $colno){ print( substr($seq, $b, $colno), "\n"); } } exit;

The bottom line is that the code you posted was not handing in what you thought it was to the pretty_print() sub. $colno wants the number of columns to print on a given line...

Do take the time to learn about the Perl debugger, always 'use strict' and 'use warnings', and use this site and perldoc perltut to help you along. Hope that gets you started...

...the majority is always wrong, and always the last to know about it...

A solution is nothing more than a clearly stated problem...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201462]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found