Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

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

kennethk has provided great targeted script advice. Here's another coding option:

use Modern::Perl; my ( $str, %positions ) = 'BATCATDATEFEAT'; $str =~ s/(A|T)/$positions{pos($str)+1}=$1/ige; open my $fh, '>', 'Results.txt' or die $!; say $fh "Position of $positions{$_} ends at $_" for sort { $a <=> $b } keys %positions; close $fh;

Output to file:

Position of A ends at 2 Position of T ends at 3 Position of A ends at 5 Position of T ends at 6 Position of A ends at 8 Position of T ends at 9 Position of A ends at 13 Position of T ends at 14

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:

use Modern::Perl; use Sort::Naturally; my ( $str, %i, %positions ) = 'BATCATDATEFEAT'; $str =~ s/(A|T)/$positions{$1 . '=' . ++$i{$1}}=pos($str)+1/ige; open my $fh, '>', 'Results.txt' or die $!; say $fh "$_ ends at $positions{$_}" for nsort keys %positions; close $fh;

Output to file:

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

Here, Sort::Naturally is used to sort the keys which have mixed content, e.g., A=1.

In reply to Re: How can I get the results in a text file from counting in a string? by Kenosis
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 avoiding work at the Monastery: (9)
    As of 2018-02-21 16:16 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (283 votes). Check out past polls.