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


In reply to Re: How to use this subroutine by wjw
in thread How to use this subroutine by bisimen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.