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

Question on format statement

by perl_seeker (Scribe)
on Aug 03, 2010 at 12:21 UTC ( [id://852647]=perlquestion: print w/replies, xml ) Need Help??

perl_seeker has asked for the wisdom of the Perl Monks concerning the following question:


this is the code i've been trying to execute. A lot of it conributed by roboticus in a previous post.
$mr_date = localtime(); open FS, ">mmonthlyfancy.doc" or die "Can't open file"; format FS = @<<<<<<<<<<<<<<<<<<<<<<<< @#### @<< @<< @# @###### @##### @<<<< +<<<<<< Rs.@####.## ~ $consname, $consno, $subdvn, $tow, $phase, $chequeno, $receiptno, $dat +e, $amount . format FS_TOP = @|||||||||||||||||||||||||||||||||||| Pg @< "MONTHLY METER WORKS REPORT", $% Date @<<<<<<<<<<<<<<<<<<<<<<< $mr_date Consumer name CN. Sub. TOW Ph. RN Date Amt. ------------- ---- ---- --- --- ------- ---- ---- ---- . format TOTAL= ------------------------------------------------------------ + Rs.@########.## $fb_rp_1_tot + Signature . open(MI,"<mmonthlyinputt.txt"); @mr_rows = <MI>; close(MI); $fb_rp_1_tot = 0; $fbqty1= 0; foreach (@mr_rows) { chop(); ($consname, $consno, $subdvn, $tow, $phase, $chequeno, $receiptno, $date, $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); $date = "" if !defined($date); $amount= 0 if !defined($amount); $sub1='FB'; $tow1='RP'; $ph1='1'; if (($subdvn eq $sub1) && ($tow eq $tow1) && ($phase eq $ph1)) { write(FS); $fb_rp_1_tot += $amount; $fbqty1 = $fbqty1 + 1; } } my $dofh = select(FS); $~ = "TOTAL"; select($dofh); write(FS); close(FS);
I keep getting this error (ugh)
Format not terminated at C:\Perl\programs\format line 92, at e +nd of line syntax error at C:\Perl\programs\format line 92, at EOF Execution of C:\Perl\programs\format aborted due to compilatio +n errors.
What is causing the problem? Thanks in advance


Replies are listed 'Best First'.
Re: Question on format statement
by jethro (Monsignor) on Aug 03, 2010 at 12:36 UTC

    The '.' to end a format must be in the first column of the line, no space before it.

    You can read about the gory details of format by entering 'perldoc perlform' on the command line

      Thanks its works,,
Re: Question on format statement
by almut (Canon) on Aug 03, 2010 at 12:32 UTC

    My guess would be that there's whitespace after before the dot at the end of the format specification.

Re: Question on format statement
by afoken (Chancellor) on Aug 03, 2010 at 12:38 UTC

    Whitespace. Quoting perlform:

    A single "." in column 1 is used to terminate a format.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      It was the space before "." causing the problem. Putting it in the 1st column solved the problem. Thanks a lot everyone.

      perl seeker


      I however want to ask another question regarding the code. Is it possible to use several other conditional checks (if statements) to write separate blocks, of data onto the output .doc file? Using the same code I mean. Putting another if statement inside the for loop does not work and the output data rows get mixed up. For example:
      $sub1='FB'; $tow1='RP'; $ph3='3'; if (($subdvn eq $sub1) && ($tow eq $tow1) && ($phase eq $ph3)) { write(FS); $fb_rp_3_tot += $amount; $fbqty3 = $fbqty3 + 1; }
      Is there something like a case statement in Perl? Any help would be appreciated.


        I'm not quite sure what you're asking here. But for a case statement, I just use the simple tried and true:

        if ('foo' eq $val) { # process case 1 } elsif ('bar' eq $val or 'baz' eq $val) { # process case 2 } elsif ('boom' eq $val) { # process case 3 } else { print "Unexpected value '$val'!\n"; }

        Just add elsif blocks as needed. Not as "pretty" as a switch/case statement in other languages, but it's simple and clear.

        The way your question is phrased makes me think you might have meant "how do you use different format statements within a page?". To do that, you do something like:

        use FileHandle; format foo_rec = FOO @>>>>>>> $flds[3] . format dflt_rec = @<<<<<<< @>>>>>>> @>>>>> @>>>>> $flds[2] $flds[3] $flds[5] $flds[9] . while (<FH>) { my @flds = split /\|/, $_; if ($flds[0] eq 'foo') { format_name FS "foo_rec"; write FS; } else { format_name FS "dflt_rec"; write FS; } }


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://852647]
Approved by almut
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 18:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found