I have the following code and the following two situations, the code strips a pair of numbers from a string and adds them together and writes to a report the details of the operation (100 rounds of incremental iteration for $num1, $num2) using formats then it asks if the user wants to run another round by providing a string containing another pair of numbers (I would still have to work on the if conditional at the end for better functionality anyways). The code works fine when run in DOS shell but
Situation1: when I tried to associate with the write function another filehandle t +han STDOUT to make it write the report to a file it tells me the file +handle isn't defined, uncomment the lines 32 and 33 to see that.
Situation2: for every run of the report I wanted to make the program start a new e +pisode on another page (i.e it starts a new episode at page 3 if it s +topped at page 2)
I couldn't recognize where I went wrong for the first situation, and for the other situation I could not figure how to manipulate the $%, $= and $- variables. So I would welcome your suggestions .
#!/usr/local/bin/perl use strict; use warnings; my($title,$num1, $num2, $result); format report_TOP= @||||||||||||||@>>>>>>>> $title , "Page $%" --------------------------------- NUMBER1 || NUMBER2 || ADDITION --------------------------------- . format report= @###@#########@############ $num1, $num2, $result . $~="report"; $^="report_TOP"; $title = "Some Report"; START: print "Please Enter a text containing pairs of numbers\n"; while(<>){ if(/^(exit|quit)$/){exit;} ($num1, $num2) = (/(\d+)/g) for ($_); add($num1, $num2); } sub add{ # open FILE,'>REPORT.txt' or die "could not open it $!\n"; # select FILE; for(my $index=0;$index<100;$index++){ $result = $num1 + $num2; write; $num1++; $num2++; } print "another Pair of numbers? Y N\n"; if(<> =~ /^N$/i){ exit; }else{ print "Please Enter a text containing pairs of numb +ers\n"; "START"; } }
Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

In reply to Writing to a file using Formats by biohisham

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.