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

Anyone familiar with the format routine?
I'm trying to fomat this data and align each column by using \t, but I think format would be a better solution.
I've read the docs, but they were not that helpful
-------------------------------------------------- NAME LAST COUNT -------------------------------------------------- foo bar 3 foo2 bar2 4 foo3 bar3 5

Replies are listed 'Best First'.
(jeffa) Re: using format to align
by jeffa (Bishop) on Mar 27, 2003 at 15:11 UTC
    Play around with this, then read the docs again.
    use strict; use warnings; my @row; my @stuff = ( [qw(foo bar 3)], [qw(foo2 bar2 4)], [qw(foo3 bar3 5)], ); for (@stuff) { @row = @$_; write; } format STDOUT_TOP = ---------------------------------- NAME LAST COUNT ---------------------------------- . format STDOUT = @<<<<< @||||| @### @row .
    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Just one more questions,
      Is it possible to dump the entire contents of an array in a cloumn instead of iterating through each one?
        I am not sure i follow you ... do you mean the contents of the 'container' @stuff or @row? Or do you mean you want to print out columns instead of rows? I chose @row because i am lazy ;). I could have written it like so (only pertinent code shown - untested):
        my ($foo,$bar,$baz); for my $row (@stuff) { ($foo,$bar,$baz) = @$row; write; } format STDOUT = @<<<<< @||||| @### $foo,$bar,$baz
        So, if i am reading your question correctly, the answer is no. You have to iterate through each row. Now, if you are wanting to use a 'landscape' style output instead of a 'portrait' style output, you will need to do a lot more work. Formats won't transpose 2-D arrays for you, but Math::Matrix will:
        use Data::Dumper; use Math::Matrix; my $a = Math::Matrix->new( [1,0,0], [1,0,0], [1,0,0], ); print Dumper $a->transpose();
        Hope this helps ...

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
      Thanks,
      That was very helpful :)
Re: using format to align
by SysApe9000 (Acolyte) on Mar 27, 2003 at 16:48 UTC
    I would highly recommend you avoid using FORMAT in favor of learning printf()/sprintf(). printf() is available in many languages so you might find that knowing it is worth the effort.

    Also, I think I've read that FORMAT is on the way out... but I couldn't find any mention of it in a brief search so YMMV.

    Try: perldoc -f printf to get started.

      For this problem, (s)printf indeed might be the better solution. But even if formats are "on the way out", i will still use them if they are the right fit for the problem, and sometimes they really are better than (s)printf. See Are formats in perl useful? for an interesting thread on the topic.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
      Just to correct a wrong impression, format is not going away completely in Perl 6. It is just leaving the core as a built-in with its own wierd syntax, and becoming a function and/or a module.

      This is documented under RFC 230. TheDamian may possibly have something more to say, as he is the author of the RFC, and is involved in the development.

        As rinceWind said (and as Larry made official in Apocalypse 6), in Perl 6 formats are being reworked under my evil influence. For a taste of what they'll be like, see the Text::Reform module.

        Using that module, your table would be generated like so:

        use Text::Reform; my @name = qw(foo foo2 foo3); my @last = qw(bar bar2 bar3); my @count = qw( 3 4 5 ); print form "--------------------------------------------------", "NAME LAST COUNT", "--------------------------------------------------", "[[[[[[[[[ [[[[[[[[[[[[[[[[[[ |||||", \@name, \@last, \@count;
        Or, if your data was likely to spill over multiple pages, you might prefer more sophisticated headers and footers:
        use Text::Reform; my @name = (qw(foo foo2 foo3)) x 20; my @last = (qw(bar bar2 bar3)) x 20; my @count = (qw( 3 4 5 )) x 20; print form { header => "--------------------------------------------------\n" . "NAME LAST COUNT\n" . "--------------------------------------------------", footer => sub { my ($pagenum, $lastpage) = @_; return "\n\n\nEND OF REPORT" if $lastpage; form "\n\n\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", ".../".($_[0]+1); }, pagelen => 20, pagefeed => "\n\n".("_"x60)."\n\n", }, "[[[[[[[[[ [[[[[[[[[[[[[[[[[[ |||||", \@name, \@last, \@count;
        Note in particular the use of a (conditional, nested) call to form within the footer of the main form.

        The Perl 6 format syntax will be even more sophisticated than that provided by Text::Reform, since I'll be taking advantage of new features of Perl 6. For example, the named options that control headers/footers/etc won't need to be in hashes, and array arguments won't require an explicit backslash.

        There will also be additional formatting modes and options, all of which will eventually be described in Exegesis 7.

      If FORMAT is going away, what about the language name? Sounds like it would be PEL v1.0
        You mean somebody actually still uses formats? Whaugh, I can hardly believe it!

        Formats were obsolete from the moment variable-pitch fonts became commonplace. I write my reports, if you can call them that, to text-separated files, Excel spreadsheets, or HTML tables. I've never ever used formats in my 8 years of intensive use of Perl, and I'm not planning on ever using them, either.

Re: using format to align
by jasonk (Parson) on Mar 27, 2003 at 15:09 UTC

    In what way were the docs not helpful? What did you try that didn't work?


    We're not surrounded, we're in a target-rich environment!