Hello. I am trying to generate a report using the following code
$met_date = localtime(); open MS, ">mmonthlyreport.txt" or die "Can't open file"; format MS = @<<<<<<<<<<<<<<<<<<<<<<<< @#### @<< @<< @# @###### @##### Rs.@ +####.## ~ $consname, $consno, $subdvn, $tow, $phase, $chequeno, $receiptno, $a +mount . format MS_TOP = @|||||||||||||||||||||||||||||||||||| Pg @< "Monthly Meter Works Report", $% Date @<<<<<<<<<<<<<<<<<<<<<<< $met_date Consumer name CN. Sub. TOW Ph. Chq.no. RN Amt. -------------- ---- ---- --- --- ------- ---- ---- +- . format TOTAL = ---------------------------------------------------------------------- +------------- Rs@# +#######.## $tot +al + + + Signature . open(MDAT, "<mmonthlyinput.txt"); @msrows = <MDAT>; close(MDAT); $total = 0; foreach (@msrows) { chop(); ($consname, $consno, $subdvn, $tow, $phase, $chequeno, $receiptno, + $amount) = (split(/!/)); $consname = "" if !defined($consname); $consno = 0 if !defined($consno); $subdvn = "" if !defined($subdvn); $tow = "" if !defined($tow); $phase = "" if !defined($phase); $chequeno = 0 if !defined($chequeno); $receiptno = 0 if !defined($receiptno); $amount= 0 if !defined($amount); if ($subdvn=='KP' && $tow=='RP' && $phase=='1' ) { my $kp = select(MS); $~ = "TOTAL"; select($kp); write(MS); close(MS); } }
The sample input:
person1!8966!KP!RP!3!0!2043!4800.00 person2!9664!FA!RP!3!0!2044!4800.00 person3!1490!KP!RP!1!0!2045!1330.00 person4!1731!CH!RP!1!0!2046!1330.00 person5!3061!KP!RP!1!0!2047!1250.00 person6!0835!FB!RP!3!0!2048!4800.00
The if statement does not work and all records get written onto output file.
What is the mistake here?

In reply to Format statement and 'if' loop by perl_seeker

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!
  • 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, details, 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, summary, 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.