Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Tidying Up Tabbed Printing

by neversaint (Deacon)
on May 01, 2008 at 01:46 UTC ( #683828=perlquestion: print w/replies, xml ) Need Help??

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

Dear Masters,
How can I tidy up the print output from this:
D. persimilis ggacaata D. grimshawi tcg----- D. simulans gg---agc D. willistoni aaataaat D. virilis tcg----- D. sechellia -------- D. mojavensis gca----- D. melanogaster gagggact D. erecta gagggacg D. yakuba aagggagg D. pseudoobscura ggacaata =====
Into this:
D. persimilis ggacaata D. grimshawi tcg----- D. simulans gg---agc D. willistoni aaataaat D. virilis tcg----- D. sechellia -------- D. mojavensis gca----- D. melanogaster gagggact D. erecta gagggacg D. yakuba aagggagg D. pseudoobscura ggacaata =====
The code I use to print the "untidy" version is this:
foreach my $winno ( 0 .. $highest ) { SP: foreach my $sp2 ( keys %enum_repo ) { my $lmer = $enum_repo{$sp2}[$winno]; if ( $enum_repo{$sp2}[$winno] ) { print "$sp2\t\t$lmer\n"; # PRINTING HERE } } print "=====\n\n"; }


---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Tidying Up Tabbed Printing
by grep (Monsignor) on May 01, 2008 at 02:07 UTC
    The quickest to implement would be using printf.
    #!/usr/bin/perl use strict; use warnings; foreach (<DATA>) { chomp; my @strings = split; printf("%-1s %-13s\t%8s\n", @strings); } __DATA__ D. persimilis ggacaata D. grimshawi tcg----- D. simulans gg---agc D. willistoni aaataaat D. virilis tcg----- D. sechellia --------

    But you also have the option of using formats. Using a format would be an advantage if you need a repeated header and/or footer. If you don't then I would stick with printf.

    UPDATE: clarified printf vs. format advantages

    grep
    One dead unjugged rabbit fish later...
      Text::Table is also to your service.
      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
Re: Tidying Up Tabbed Printing
by Fletch (Chancellor) on May 01, 2008 at 02:39 UTC

    Perhaps also Text::Reform?

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Tidying Up Tabbed Printing
by jethro (Monsignor) on May 01, 2008 at 02:19 UTC
    my $tab=20; print substr($sp2 . ' ' x $tab,0,$tab),$lmer,"\n";
Re: Tidying Up Tabbed Printing
by apl (Monsignor) on May 01, 2008 at 09:49 UTC
Re: Tidying Up Tabbed Printing
by moklevat (Priest) on May 01, 2008 at 13:21 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://683828]
Approved by grep
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2019-11-13 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (74 votes). Check out past polls.

    Notices?