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

Hey guys I have been out of the perl loop for some time now and unfortunatly I feel quite rusty. Anyway I need to do some formatting of data where each line is in the same format, and I have a few section that need to be left or right justified and then padded with whitespace.

I think I remember doing this sort of stuff with HEREDOCS (I think that was what it was called) where you do a bunch of <<<<<<<< <<<< <<< and then when you use that format it makes that each one is the correct ammount of spaces.

As an example if I were to use that format and have this set of data "one" "two" "b" I would get "one      two b   " (Quotes just used to put emphasis on white space).

Unfortuantly, either HEREDOCS is not the right term (which would explain why my searches aren't turning up anything useful) or using HEREDOCS have come out of use. Any help would be appreciated. Thanks.

Edited by Chady -- code tags around whitespaced string

Replies are listed 'Best First'.
Re: Formatting Question
by tinita (Parson) on May 27, 2004 at 12:47 UTC

      It was format. Thanks. I guess another quick question would be is there a way to abbreviate the number of <<'s or >>'s. Like I have filled that is just 100 characters of filler (whitspace) it seems silly to have to put 100 >'s.

      As I am writing this I am thinking maybe printf would be better. But can you left and right justify in printf. I am not sure.

        $_ = 'justify my love'; printf "[%20s] and [%-20s]\n", $_, $_;
        will give you
        [ justify my love] and [justify my love ]
        You can actually combine sprintf and formats, if you want to avoid specifying the width as a picture. You can use @* as your format for a line, and do all the actual formatting of the line with a sprintf. That assumes that you have some reason for using format/write instead of doing it all with printf.

        The PerlMonk tr/// Advocate
Re: Formatting Question
by Ninthwave (Chaplain) on May 27, 2004 at 12:53 UTC
Re: Formatting Question
by Happy-the-monk (Canon) on May 27, 2004 at 12:58 UTC