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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How can I get the results in a text file from counting in a string?
by kennethk (Abbot) on Aug 07, 2012 at 17:39 UTC | |
by ww (Archbishop) on Aug 07, 2012 at 18:35 UTC | |
by kennethk (Abbot) on Aug 07, 2012 at 20:34 UTC | |
Re: How can I get the results in a text file from counting in a string?
by trizen (Hermit) on Aug 07, 2012 at 17:55 UTC | |
Re: How can I get the results in a text file from counting in a string?
by Kenosis (Priest) on Aug 07, 2012 at 19:16 UTC | |
Re: How can I get the results in a text file from counting in a string?
by johngg (Canon) on Aug 07, 2012 at 22:30 UTC |