http://www.perlmonks.org?node_id=986033

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:

#!/usr/bin/perl -w $str="BATCATDATEFEAT"; $A=0;$T=0; while ($str=~ /A/ig) {$A++;# Line 4 print"\n A=$A ends at ",pos $str,"\n";} while ($str=~ /T/ig) {$T++; print"\n T=$T ends at ",pos $str,"\n";} $output="Results .txt"; # Line 8 unless (open(RESULT,">$output")){ print"Cannot open file\"$output\".\n\n"; exit; # Line 11 } # Line 12 print RESULT"\n A=$A ends at ",pos $str,"\n T=$T ends at ",pos $str,"\n\n"; close(RESULT); # Line 17 exit;

I have got the correct results in cmd screen:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\xx>cd desktop C:\Users\xx\Desktop>at.pl A=1 ends at 2 A=2 ends at 5 A=3 ends at 8 A=4 ends at 13 T=1 ends at 3 T=2 ends at 6 T=3 ends at 9 T=4 ends at 14 Use of uninitialized value in print at C:\Users\DR-SUPRIYO\Desktop\at. +pl line 13. Use of uninitialized value in print at C:\Users\DR-SUPRIYO\Desktop\at. +pl line 13.

Output Text File in Desktop shows the wrong results:

A=4 ends at T=4 ends at