Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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:

#!/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 $A=0;$T=0; while ($str=~ /A/ig) {$A++;# Line 4 print RESULT "\n A=$A ends at ",pos $str,"\n";} while ($str=~ /T/ig) {$T++; print RESULT "\n T=$T ends at ",pos $str,"\n";} close(RESULT); # Line 17 exit;
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:

#!/usr/bin/perl -w use strict; my $str="BATCATDATEFEAT"; my $A=0; my $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"; } my $output="Results.txt"; # Line 8 open(my $fh, ">", $output) or die "Cannot open file '$output'.\n\n"; $A=0; $T=0; while ($str=~ /A/ig) { $A++; print $fh "\n A=$A ends at ",pos $str,"\n"; } while ($str=~ /T/ig) { $T++; print $fh "\n T=$T ends at ",pos $str,"\n"; }

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: How can I get the results in a text file from counting in a string? by kennethk
in thread How can I get the results in a text file from counting in a string? by supriyoch_2008

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2014-10-01 23:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (41 votes), past polls