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

It seems easy to use the "~" to make a format optional for write()...but it doesn't seem to work if I use formline()

I basically want to use a format to save in a string, not a filehandle and I would like to handle the case of text wrapping over...

$^A = ""; formline(<<'OUTPUT_TABLE', $1, $2); @<<<<<<< @<<<<<<<<<<<< ~@<<<<<<< @<<<<<<<<<<<< OUTPUT_TABLE $out .= $^A; print $out...


thanks for any suggestions !

Replies are listed 'Best First'.
Re: format question
by jmcnamara (Monsignor) on Nov 08, 2001 at 14:23 UTC

    The code you posted works for me if $1 and $2 have been set.

    However, you have 4 placeholders and only 2 variables.

    --
    John.

      the second "format" line, that starts with ~ is supposed to be optional if there is no more data from $1 or $2. It would work if I used "format =", but I am using formline(), and it doesn't seem to...

      thanks

      ps
      I posted this pretty late last night...is there a way to resubmit it now that there are more people logged on? I don't want to abuse this GREAT system.

        Okay, I think I see the cause of the confusion. The tilde ~ does eliminate blank lines. However, setting $^A = "" does not reset the accumulator. You have to print it to do that.

        Consider the following example based on your code snippet:

        #Something tells me we're not programming in Perl anymore Toto. ;-) for (1..2) { print "Run: ", $_, "\n"; $^A = ""; formline(<<'OUTPUT_TABLE', 1, 2); @<<<<<<< @<<<<<<<<<<<< @<<<<<<< @<<<<<<<<<<<<~ OUTPUT_TABLE $out .= $^A; print $out; } for (3..4) { print "Run: ", $_, "\n"; $^A = ""; formline(<<'OUTPUT_TABLE', 1, 2); @<<<<<<< @<<<<<<<<<<<< @<<<<<<< @<<<<<<<<<<<<~ OUTPUT_TABLE print $^A; }
        This gives the following output. Note the additional line in Run 2.

        Run: 1 1 2 Run: 2 1 2 1 2 Run: 3 1 2 Run: 4 1 2
        Therefore, you will have to re-organise your code to make sure that you print $^A.

        --
        John.