Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Using '#' at the beginning of a format?

by loris (Hermit)
on Jan 10, 2013 at 09:20 UTC ( #1012623=perlquestion: print w/ replies, xml ) Need Help??
loris has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I would like to use 'format' to print out some stuff. However, in the top format, I would like to use '#' at the beginning of the lines, e.g.

# Name Price # ---- ----- large 90.12 small 12.34 medium 57.78

As you may imagine, googling for 'perl format hash' or similar isn't much fun. Anyone know the answer?

Thanks,

loris

Comment on Using '#' at the beginning of a format?
Download Code
Re: Using '#' at the beginning of a format?
by Anonymous Monk on Jan 10, 2013 at 09:27 UTC
      uh uh! never eard of it ... i remember it was better NOT use formats in perl 5. This modules is safe to use?


      there are no rules, there are no thumbs..

        Perl6::Form is a lot better than format. The usage is a lot easier.
        loris request can be done like so:

        use warnings; use strict; use Perl6::Form; my %name_n_price_tag; print form "#\tName\tPrice", "#\t------ -------"; while (<DATA>) { chomp; my ( $name, $price ) = split /:\s/, $_, 2; $name_n_price_tag{$name} = $price; } print form "\t{|||||}\t{|||||}", $_, $name_n_price_tag{$_}, $/ for sort keys %name_n_price_tag; __DATA__ large: 90.12 small: 12.34 medium: 57.78
        Output:
        # Name Price # ------ ------- large 90.12 medium 57.78 small 12.34
        NOTE however, I don't know how the OP gets it data. This is just a head up using the module Perl6::Form.
        Hope this helps.

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me
Re: Using '#' at the beginning of a format?
by Marshall (Prior) on Jan 10, 2013 at 12:44 UTC
    When I first saw the Perl format thing, I thought it would be very cool. Well as it turns out, using printf() is usually far better for any kind of normal report.
    #!/usr/bin/perl -w use strict; my @stuff = qw( large 90.12 small 12.34 medium 57.78 super 75.0 ); print "# Name Price\n", "# ---- -----\n"; while (my ($name,$price) = splice (@stuff,0,2)) { printf "%10s %8.2f\n", $name, $price; } __END__ Prints: # Name Price # ---- ----- large 90.12 small 12.34 medium 57.78 super 75.00
Re: Using '#' at the beginning of a format?
by Tux (Monsignor) on Jan 10, 2013 at 13:34 UTC
    use 5.14.1; use warnings; format STDOUT_TOP = @ Size Price "#" @ ---- ----- "#" . format STDOUT = @>>>>>> @###.## $a, $b . my %pizza = ( Small => 12.34, Medium => 57.78, Large => 90.12, ); while (($a, $b) = each %pizza) { write; } => # Size Price # ---- ----- Small 12.34 Medium 57.78 Large 90.12

    Enjoy, Have FUN! H.Merijn
Re: Using '#' at the beginning of a format?
by sundialsvc4 (Monsignor) on Jan 10, 2013 at 16:18 UTC

    Actually, what I normally do is to generate the report output as an XML file, then run it through XSLT ... or simply let the web browser do that work for me.   Simply adopt a consistent structure for the XML data that you intend to report.   (Going so far as to define a schema that can be validated is probably a good thing.)   Then, set up an XSLT stylesheet that can be used to turn all of them into good-looking output with CSS stylesheets and so forth.   It can do a lot of things for you which you therefore don’t have to do in code, and it looks a heck of a lot more professional than “line-printer art.”   Using XML also makes the report output in-general more of a resource, that can be used for other things.

    (If you want to see just what this technology is capable of, look at this interactive table of the elements which was created in this way ... virtually no programming required.)

      LOL

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012623]
Approved by Corion
Front-paged by 2teez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-07-12 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (241 votes), past polls