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

im trying to figure something out here that doesnt make sense! im outputting 3 rows of arrays to the screen and they are formatted correctly so that it looks like a table: ie:
Range 10-20 20-30 30-40 41-50 51-60 61-70 Count 0 0 0 0 3 0 Percent 0 0 0 0 75 0
however when i output exactly the same stuff into a txt file it comes up as something like this:
Range 10-20 20-30 30-40 41-50 51-60 61-70 Count 0 0 0 0 3 0 Percent 0 0 0 0 75 0
im using the "\t" in a foreach loop to seperate the values in the array. can i do this in some other way so that when it gets outputted to the file it looks the same as it does when outputted on the screen???

Replies are listed 'Best First'.
Re: output of array into data
by Fletch (Bishop) on Sep 06, 2006 at 14:35 UTC

    Whatever you're using to view the text file with has a different notion of tab width than your screen. Correct it's notion, or use something like Text::Reform to lay it out without tabs.

Re: output of array into data
by chargrill (Parson) on Sep 06, 2006 at 14:36 UTC

    You could try using Text::Table, and it would handle that for you.



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
Re: output of array into data
by mreece (Friar) on Sep 06, 2006 at 14:51 UTC
    your text editor is set to use 4 columns for tabs. you will have to use spaces instead. printf is one way:
    my @data = ( [qw( Count 0 0 0 0 3 0 )], [qw( Percent 0 0 0 0 75 0 )] ); my @headers = qw(Range 10-20 20-30 30-40 41-50 51-60 61-70); printf "%-10s %7s %7s %7s %7s %7s %7s\n", @headers; foreach my $values ( @data ) { printf "%-10s %7d %7d %7d %7d %7d %7d\n", @$values; }
    produces:
    Range        10-20   20-30   30-40   41-50   51-60   61-70
    Count            0       0       0       0       3       0
    Percent          0       0       0       0      75       0
    
Re: output of array into data
by svenXY (Deacon) on Sep 06, 2006 at 14:40 UTC
    Hi,
    good old format should also do the trick for you.
    Regards,
    svenXY
      My thoughts exactly. Here's a related link.

      ---
      It's all fine and dandy until someone has to look at the code.
Re: output of array into data
by artist (Parson) on Sep 06, 2006 at 20:23 UTC
    use Text::Table; my $tb = Text::Table->new(); @Range = qw(10-20 20-30 30-40 41-50 51-60 61-70); @Count = qw(0 0 0 0 3 0); @Percent = qw(0 0 0 0 75 0); my @data = ( [ "Range", @Range ], [ "Count", @Count ], [ "Percent", @Percent ], ); $tb->load(@data); print $tb;
    Above code produces
    Range   10-20 20-30 30-40 41-50 51-60 61-70
    Count   0     0     0     0      3    0
    Percent 0     0     0     0     75    0
    
    --Artist