supriyoch_2008 has asked for the
wisdom of the Perl Monks concerning the following question:

Hi Perl Monks,

I am a beginner in perl. I am interested in counting the
number and position of A's and T's in the string $str.
My perl code gives correct results in the cmd
screen. But the output in the text file does not show the results
(possibly the pos function has to be assigned to a variable). I have
used the pos function as cited in the PERLQUICK Tutorial (page 8;
Example: $x="cat dog house"; and in the Perlretut Tutorial (page:32;
Example: while ($dna=~ /(\w\w\w)*?TGA/g){print" Got a TGA stop codon
....}. But I am not getting the results of the cmd screen as a text output
on desktop. Would you suggest me how to get the results in a text file?
My code goes as:

While there are some cosmetic issues I'd like to raise, the heart of your issue with the posted code is that you have not wrapped your output-to-file call to pos in the same while loop that you wrapped your output-to-screen block. Except, of course, then you try to output them both simultaneously, which is problematic because the first time through, you used two different loops. The following does what (I think) you intend:

with minimal changes to code. Note that this opens a file with a white space in the name, which is likely not what you intended and makes dealing with files generally a bigger pain.

You may want to check out open and die for some more readable code, strict for making debugging easier, and you may want to add line breaks and indents to make flow more obvious. So a cleaner version of this code may look like:

While I certainly think the code could use dramatic algorithmic improvement, I was attempting to keep the logic as consistent as possible, given the OP's apparent level of familiarity. My actual inclination would be to write it as a single pass, stashing results in a hash, so you could separate the processing and the output. Also reduces duplication of code. But to each their own, premature optimization, and all that jazz.

This option uses the e modifier in the substitution to execute the embedded code, which holds position/letter (key/value) pairs in a hash. The printing part numerically sorts the keys, sending the output to Results.txt.

Hope this helps!

Update: by modifying the script a little, it can generate your output:

When putting a smiley right before a closing parenthesis, do you:

Use two parentheses: (Like this: :) )
Use one parenthesis: (Like this: :)
Reverse direction of the smiley: (Like this: (: )
Use angle/square brackets instead of parentheses
Use C-style commenting to set the smiley off from the closing parenthesis
Make the smiley a dunce: (:>
I disapprove of emoticons
Other