Formatting output in columns: 
Author Message
 Formatting output in columns:

I thouroughly searched newsgroups and faq, but I couldn't find the
answer to my question, although I saw references that led me
to believe that it can be easily done.

I have following array:


          str8, str9, str12 ...)

I need to format output text in following fashion:

str1  str5  str9     ...
str2  str6  str10
str3  str7  str11
str4  str8  str12

How can i do that in perl?

thank you very much!!!



Mon, 19 Sep 2005 18:02:24 GMT  
 Formatting output in columns:

Quote:

> I thouroughly searched newsgroups and faq, but I couldn't find the
> answer to my question, although I saw references that led me
> to believe that it can be easily done.

> I have following array:


>           str8, str9, str12 ...)

> I need to format output text in following fashion:

> str1  str5  str9     ...
> str2  str6  str10
> str3  str7  str11
> str4  str8  str12

> How can i do that in perl?

> thank you very much!!!

i'm sure there's modules to do that, but in straight perl, here's one
way: (i've made some assumptions, but it compiles and runs)

my $rows = 5;


               str7 str8 str9 str10 str11 str12/;    


# calc max col width
my $max;

  $max = length($_) unless $max > length($_);

Quote:
}

# allow for margins between columns
my $colwidth = $max + 2;

# print it!
for my $r (0..($rows - 1)) {
  last unless $array[$r];
  for my $c (0..$cols) {
    printf "%-${colwidth}s", $array[$r + ($rows * $c)];
  }
  print "\n";

Quote:
}

yields:

str1   str6   str11  
str2   str7   str12  
str3   str8          
str4   str9          
str5   str10        

hth-

--
Michael Budash



Mon, 19 Sep 2005 18:39:08 GMT  
 Formatting output in columns:

Quote:

> I thouroughly searched newsgroups and faq, but I couldn't find the
> answer to my question

I don't believe you.  'perldoc -q format' gives you _excactly_ what you're
after.

Quote:
> I have following array:


>           str8, str9, str12 ...)

No you don't.

--

"You know the world is going crazy when the best rapper is white, the best
 golfer is black, France is accusing US of arrogance and Germany doesn't
 want to go to war."



Mon, 19 Sep 2005 20:17:42 GMT  
 Formatting output in columns:

Quote:


> > I thouroughly searched newsgroups and faq, but I couldn't find the
> > answer to my question

> I don't believe you.  'perldoc -q format' gives you _excactly_ what
> you're after.

> > I have following array:


> >           str8, str9, str12 ...)

> No you don't.

Actually, ignoring the "..." at the end, which is obviously a
placeholder for other data, this is perfectly valid perl code -- if you
don't have "use strict" in your code.

--





Tue, 20 Sep 2005 06:19:33 GMT  
 Formatting output in columns:

Quote:


> > I thouroughly searched newsgroups and faq, but I couldn't find the
> > answer to my question

> I don't believe you.  'perldoc -q format' gives you _excactly_ what
> you're after.

I didn't see any such answer under 'perldoc -q format'.

Maybe you meant "perldoc perlform" ?

Although write() and 'format NAME = ...' have been part of perl since
nearly the very beginning, nowadays they are a very obscure feature of
perl.

Futhermore, there's nothing there for putting data *vertically* in
columns, as opposed to horizontally.

That is, you can easily get:
   a b c
   d e f
   g h i
   j
But there's nothing describing how to get:
   a e i
   b f
   c g
   d h

Also, the OP's sample output implied that there should be a total of 4
rows, regardless of how many items there were, which makes the use of
write() rather hard.

The easiest solution is to process a column at a time, adding a bit at a
time to each of 4 rows, then print out the rows.

   use strict;
   use warnings;


   $m += 2;


      $rows[$_] .= sprintf "%-${m}s", $col[$_]
         for 0 .. $#col;
   }

--  





Tue, 20 Sep 2005 07:05:32 GMT  
 Formatting output in columns:

Quote:



> > > I thouroughly searched newsgroups and faq, but I couldn't find the
> > > answer to my question

> > I don't believe you.  'perldoc -q format' gives you _excactly_ what
> > you're after.

> I didn't see any such answer under 'perldoc -q format'.

> Maybe you meant "perldoc perlform" ?

> Although write() and 'format NAME = ...' have been part of perl since
> nearly the very beginning, nowadays they are a very obscure feature of
> perl.

> Futhermore, there's nothing there for putting data *vertically* in
> columns, as opposed to horizontally.

> That is, you can easily get:
>    a b c
>    d e f
>    g h i
>    j
> But there's nothing describing how to get:
>    a e i
>    b f
>    c g
>    d h

> Also, the OP's sample output implied that there should be a total of 4
> rows, regardless of how many items there were, which makes the use of
> write() rather hard.

> The easiest solution is to process a column at a time, adding a bit at a
> time to each of 4 rows, then print out the rows.

>    use strict;
>    use warnings;


>    $m += 2;


>       $rows[$_] .= sprintf "%-${m}s", $col[$_]
>          for 0 .. $#col;
>    }

> --  




Thank you very much!!!!

Actually, since I had to wait for 9 hours for my original post to come
through the google, I came up with my own script which implements the
similar algorithm, but a lot less pretty:)))   I will use your code
instead

thanks again!



Tue, 20 Sep 2005 16:16:57 GMT  
 Formatting output in columns:

Quote:

>>> I thouroughly searched newsgroups and faq, but I couldn't find the
>>> answer to my question
>> I don't believe you.  'perldoc -q format' gives you _excactly_ what
>> you're after.
> I didn't see any such answer under 'perldoc -q format'.

Frustrated as I was when I wrote that message, I managed to hit the 'q'
instead of 'f'. :-)

Quote:
> Maybe you meant "perldoc perlform"?

Yes.  'perldoc -f format' points to 'perldoc perlform'.

--

"You know the world is going crazy when the best rapper is white, the best
 golfer is black, France is accusing US of arrogance and Germany doesn't
 want to go to war."



Thu, 22 Sep 2005 15:21:27 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. **FORMAT Formatting columns/pages

2. Columns from a format definition?

3. Multi-column format?

4. Format a one line file to get two columns

5. column format -easier way of doing it

6. Tricky column formatting/space removing question

7. multi column format

8. Extra spaces in output and concatenating columns

9. Retrieving TEXT column output with DBI

10. Specifying mySQL output columns from HTML form checkboxes?

11. Retrieving TEXT column output with DBI

12. Web Techniques Perl columns now available on-line (and more Unix Review columns)

 

 
Powered by phpBB® Forum Software