Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^3: Formatting Output

by kcott (Chancellor)
on Oct 19, 2012 at 10:33 UTC ( #999902=note: print w/replies, xml ) Need Help??

in reply to Re^2: Formatting Output
in thread Formatting Output

If you liked the pack solution, here's a more versatile version (with some additional test data):

#!/usr/bin/env perl use strict; use warnings; my (@data, @max); while (<DATA>) { push @data => [ split /,/ ]; @max = map { my $len = length $data[$#data][$_]; ( $max[$_] ||= 0 ) >= $len ? $max[$_] : $len; } 0 .. $#{$data[$#data]} - 1; } my $template = 'A' . join(A => ( map { $_ + 1 } @max ), '*'); print pack $template => @$_ for @data; __DATA__ Account,Login Name,Password,Web Site,Comments,Extra Google Docs,george123,F0oB@r,,The GoodleDocs + site,A Amazon,george123,fo0bA7,,The Amazone Kindle site, +B Apple,george123,mM_B39Aa,https://www. ... shorter,My Apple account,C Longer Account,Login Name,Password,Web Site,Comments,D Account,Longer Login Name,Password,Web Site,Comments,E Account,Login Name,Longer Password,Web Site,Comments,F Account,Login Name,Password,Longer Web Site,Comments,G Account,Login Name,Password,Web Site,Longer Comments,H XAccount,XLogin Name,XPassword,XWeb Site,XComments,I


$ Account Login Name Password Web Site + Comments Extra Google Docs george123 F0oB@r +/docs The GoodleDocs site A Amazon george123 fo0bA7 + The Amazone Kindle site B Apple george123 mM_B39Aa https://www. ... shor +ter My Apple account C Longer Account Login Name Password Web Site + Comments D Account Longer Login Name Password Web Site + Comments E Account Login Name Longer Password Web Site + Comments F Account Login Name Password Longer Web Site + Comments G Account Login Name Password Web Site + Longer Comments H XAccount XLogin Name XPassword XWeb Site + XComments I

You could also use this technique to create a FORMAT for printf (as suggested by toolic++ earlier).

Update: s/@max[0 .. $#max]/@max/

-- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999902]
[Corion]: marinersk: Well, I have done select statements like select sum(foo) as "Total Amount", ..., but to have a table like that makes me shudder
[Corion]: SuicideJunkie: :-D
[marinersk]: SuicideJunkie LOL
[choroba]: Woohoo! Fixed a test that hasn't run for 3 years.
[marinersk]: Corion Yes, sometimes whitespace in column headers is acceptable, but I still consider it be less than desireable if that query might get revectored for an ETL-esque process...
[marinersk]: choroba++
[choroba]: it's a long running test, so it's normally skipped unless an env var is set
[choroba]: nobody has been bothered to set the variable in the last 3 years
[marinersk]: sub newtest{my $expected_result = &target('foo'); my $actual_result = &target('foo'); if ($actual_result eq $expected_result) { &tdd_success(); } else { &tdd_fail(); } } # Test works after three years!
[choroba]: or nobody bothered...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-05-25 15:08 GMT
Find Nodes?
    Voting Booth?