Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

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

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> 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

In reply to 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: (10)
    As of 2018-03-23 17:34 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (295 votes). Check out past polls.