Click here to get back home

printing a multidimensional array in table format

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
printing a multidimensional array in table format joel 08-28-2006
Get Chitika Premium
Posted by joel on August 28, 2006, 12:28 pm
Please log in for more thread options
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,


> There
> is a rogue carriage return (0xd) in the string

> Is there something I can do to deal with this
> situation?


Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Posted by Brian McCauley on August 28, 2006, 1:02 pm
Please log in for more thread options

joel wrote:
> For any number of rows and columns of a multidimensional array, I would
> like my printHandler subroutine to print in table format with minimal
> effort. What is the easiest way of doing this?
>
> {
>         ...
>         ...
>         while (my (@array)=($sth->fetchrow_array()))

All those parentheses don't really add clarity...

        while (my @array = $sth->fetchrow_array )

>         {
>                 push(@result,[@array]);
>         }

Since @array is declared within the loop (good!) you don't need to make
a copy of it, you can push a reference to @array itself as you'll get a
new @array on the next iteration.

                push(@result,\@array);

>         &printHandler(@result);

Do you know what the & there does? If not then remove it.

As a general rule you should pass arrays to subroutines using array
references rather than unrolling the array into a list and passing each
element of the array as a separate argument.

        printHandler(\@result);

BTW: have you considered using fetchall_arrayref?

> sub printHandler
> {
>         my $i=0;
>         foreach (@_)
>         {
>                 print "@\n";
>                 $i++;
>         }
> }

Ouch! You put each element of @_ in turn into $_ and then ignore that
and hand carft a loop index to use as a subscript.

foreach (@_)
        {
                print "@\n"; # {} strictly redundant
        }

Or if you'd passed an array ref...

my ($list)= @_;
foreach (@$list)
        {
                print "@$_\n";
        }


> current output:
> 4591 05-00371
> 149817 06-02930
> 15541 06-0369
> 827 04-00307
> 13406 06-01364
>
> desired output:
> 4591 05-00371
> 149817 06-02930
> 15541 06-0369
> 827 04-00307
> 13406 06-01364

Ah, you want to print the output formatted. Perl has a "print
formatted" function ..

my ($list)= @_;
for (@$list)
        {
                printf "%6s %s\n",@$_;
        }

Or if you feel terse..

                printf "%6s %s\n",@$_ for @;


Posted by joel on August 28, 2006, 2:28 pm
Please log in for more thread options
Thanks for the detailed tips, much more than I expected. I have used
printf but it would require that I write code to determine the maximum
widths of each column (the number of columns and size being variable).
Not big deal but I thought that there may be an even easier way. Perl
is for doing things the easy way, right ;-)


Brian McCauley wrote:
> joel wrote:
> > For any number of rows and columns of a multidimensional array, I would
> > like my printHandler subroutine to print in table format with minimal
> > effort. What is the easiest way of doing this?
> >
> > {
> >         ...
> >         ...
> >         while (my (@array)=($sth->fetchrow_array()))
>
> All those parentheses don't really add clarity...
>
>         while (my @array = $sth->fetchrow_array )
>
> >         {
> >                 push(@result,[@array]);
> >         }
>
> Since @array is declared within the loop (good!) you don't need to make
> a copy of it, you can push a reference to @array itself as you'll get a
> new @array on the next iteration.
>
>                 push(@result,\@array);
>
> >         &printHandler(@result);
>
> Do you know what the & there does? If not then remove it.
>
> As a general rule you should pass arrays to subroutines using array
> references rather than unrolling the array into a list and passing each
> element of the array as a separate argument.
>
>         printHandler(\@result);
>
> BTW: have you considered using fetchall_arrayref?
>
> > sub printHandler
> > {
> >         my $i=0;
> >         foreach (@_)
> >         {
> >                 print "@\n";
> >                 $i++;
> >         }
> > }
>
> Ouch! You put each element of @_ in turn into $_ and then ignore that
> and hand carft a loop index to use as a subscript.
>
> foreach (@_)
>         {
>                 print "@\n"; # {} strictly redundant
>         }
>
> Or if you'd passed an array ref...
>
> my ($list)= @_;
> foreach (@$list)
>         {
>                 print "@$_\n";
>         }
>
>
> > current output:
> > 4591 05-00371
> > 149817 06-02930
> > 15541 06-0369
> > 827 04-00307
> > 13406 06-01364
> >
> > desired output:
> > 4591 05-00371
> > 149817 06-02930
> > 15541 06-0369
> > 827 04-00307
> > 13406 06-01364
>
> Ah, you want to print the output formatted. Perl has a "print
> formatted" function ..
>
> my ($list)= @_;
> for (@$list)
>         {
>                 printf "%6s %s\n",@$_;
>         }
>
> Or if you feel terse..
>
>                 printf "%6s %s\n",@$_ for @;


Posted by usenet on August 28, 2006, 7:54 pm
Please log in for more thread options
joel wrote:
> I have used printf but it would require that I write code to determine the
> maximum widths of each column (the number of columns and size being
> variable). Not big deal but I thought that there may be an even easier way.

There is an easy way to avoid pre-calculating your maximum field
length; let the Text::Table module do it for you:

#!/usr/bin/perl
use strict; use warnings;
use Text::Table;

my $table = Text::Table->new({align => 'left'}, {align => 'left'});

$table->load( <DATA> );
print $table;

__DATA__
4591 05-00371
827 04-00307
31415927 123-456789

--
David Filmer (http://DavidFilmer.com)


Posted by joel on August 28, 2006, 9:25 pm
Please log in for more thread options
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,


> There
> is a rogue carriage return (0xd) in the string

> Is there something I can do to deal with this
> situation?


Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Similar ThreadsPosted
Printing hash in table format June 19, 2006, 1:01 am
multidimensional Array August 9, 2007, 11:51 am
uninitialized value in multidimensional array April 17, 2008, 12:06 pm
FAQ 4.65 How can I store a multidimensional array in a DBM file? May 12, 2005, 11:03 am
FAQ: How can I store a multidimensional array in a DBM file? October 10, 2004, 5:10 pm
FAQ 4.65 How can I store a multidimensional array in a DBM file? February 13, 2005, 12:03 am
FAQ 4.65 How can I store a multidimensional array in a DBM file? July 28, 2005, 10:03 am
FAQ 4.65 How can I store a multidimensional array in a DBM file? September 17, 2005, 10:03 pm
FAQ 4.65 How can I store a multidimensional array in a DBM file? June 6, 2006, 9:03 pm
FAQ 4.65 How can I store a multidimensional array in a DBM file? July 30, 2006, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap