in reply to Re: Print Array items in Fixed width
in thread Print Array items in Fixed width


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.

Replies are listed 'Best First'.
Re^3: Print Array items in Fixed width
by BrowserUk (Patriarch) on Aug 01, 2008 at 03:38 UTC

    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.
Re^3: Print Array items in Fixed width
by broomduster (Priest) on Aug 01, 2008 at 08:37 UTC
    ... 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-)