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

Format statement and 'if' loop

by perl_seeker (Scribe)
on May 20, 2010 at 12:47 UTC ( #840914=perlquestion: print w/ replies, xml ) Need Help??
perl_seeker has asked for the wisdom of the Perl Monks concerning the following question:

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?

Comment on Format statement and 'if' loop
Select or Download Code
Re: Format statement and 'if' loop
by Anonymous Monk on May 20, 2010 at 12:58 UTC
    What is the mistake here?

    You keep doing the same thing and expecting a different output.

Re: Format statement and 'if' loop
by Fletch (Chancellor) on May 20, 2010 at 13:00 UTC

    == is for numeric comparisons; you mean eq. Go read perlop.

    Additionally: It doesn't really make any sense to close your output handle and then go on to process more rows. You probably want to use last to exit the loop and then close and print your footer outside.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Format statement and 'if' loop
by BrowserUk (Pope) on May 20, 2010 at 13:01 UTC
       if ($subdvn=='KP' && $tow=='RP' && $phase=='1' )

    Probably that you are using numeric == to compare strings.

    Of course, if you use strict & warnings, you'd now that already.

Re: Format statement and 'if' loop
by Krambambuli (Deacon) on May 20, 2010 at 13:05 UTC
    Try

    if ($subdvn eq 'KP' and $tow eq 'RP' and $phase eq '1' )

    instead of

    if ($subdvn=='KP' && $tow=='RP' && $phase=='1' )


    Krambambuli
    ---
      Ok.

      Thank you.
Re: Format statement and 'if' loop
by marto (Chancellor) on May 20, 2010 at 13:08 UTC
Re: Format statement and 'if' loop
by moritz (Cardinal) on May 20, 2010 at 13:13 UTC
    By the way there is no 'if' loop.

    if is a branching construct, not a loop. You can also say 'if' statement.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://840914]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-09-19 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (132 votes), past polls