Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Print Array items in Fixed width

by broomduster (Priest)
on Jul 31, 2008 at 08:53 UTC ( [id://701371]=note: print w/replies, xml ) Need Help??


in reply to Print Array items in Fixed width

And there's always format(perlform)... Modifying ikegami's first example slightly:

my @header = qw( TABNAME 06100 01010 06000 00110A ); my @data = qw( !W31801!-->!919!-->!90200809!-->!840!-->!MO! !W31801!-->!919!-->!902008!-->!74!-->!MO! ); my @fields; #declare here so it's visible to format below foreach my $line (@data) { @fields = split(/-->/, $line); write; } format STDOUT_TOP = @<<<<<<<<< @<<<<<<<<< @<<<<<<<<< @<<<<<<<<< @<<<<<<<<< @header . format STDOUT = @<<<<<<<<< @<<<<<<<<< @<<<<<<<<< @<<<<<<<<< @<<<<<<<<< @fields .

Replies are listed 'Best First'.
Re^2: Print Array items in Fixed width
by harishnuti (Beadle) on Aug 01, 2008 at 02:59 UTC

    yes perl formats i have tried in some of my applications, but here iam not sure how many columns my data has, the above i showed is only a fragment of my whole file, i.e. W31801 is one table with 5 headers, some other tables will have 10 or more, so i needed variable length formatting ..

    Thanks for pack functions, indeed very informative and i will note it and will implement in other requirements.

      The only real downside of pack compared with sprintf, is that pack always left justifies the fields where sprintf can do right justification also.

      If you need all the fields right justified, there is a simple trick involving 3 reverses:

      @data = qw[ the quick brown fox jumps the lazy dog ];; print pack '(A10)*', @data;; the quick brown fox jumps the lazy +dog print scalar reverse pack '(A10)*', map scalar reverse, reverse @data; +; the quick brown fox jumps the lazy + dog

      Doesn't help for mixed justification though.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      ... W31801 is one table with 5 headers, some other tables will have 10 or more, so i needed variable length formatting
      If you have "variable fixed widths", and a variable number of columns, then perlform is not the answer. But that wasn't the question you asked, either.

        correct, my question was to print table by table, so i put a question for one table sample data, now i extended this approach to all other tables, i store table data and header in hashes and print table by table, so your pack approach and ikegami's sprintf works well for me :o-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://701371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-18 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found