Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Good advice, definitely cleaner; ++.

But why require a second time thru each letter? Print to screen and immediately to file. For some, at least, this may rate as a minor improvement.

#!/usr/bin/perl use 5.014; # 986041 my $str="BATCATDATEFEAT"; my $A=0; my $T=0; my $output = 'D:/_Perl_/PMonks/986041.txt'; open(RESULT,">>$output") or die "Can't open $output, $!"; while ($str=~ /A/ig) { $A++; # OP's Line 4 print "\n A=$A ends at ",pos $str,"\n"; print RESULT "\n A=$A ends at ",pos $str,"\n"; } while ($str=~ /T/ig) { $T++; print "\n T=$T ends at ",pos $str,"\n"; print RESULT "\n T=$T ends at ",pos $str,"\n"; } close(RESULT); # originally, Line 17 exit; =head execution: C:\>type d:\_Perl_\PMonks\986041.txt 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 =cut

(Perhaps obviously) console gets identical output.

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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (4)
    As of 2018-02-21 03:48 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (274 votes). Check out past polls.