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

I'm a beginning perl programmer attempting to parse out biological data. The program gives me the data parsed out how I want it, only it does so in one long column. I want it to be in succesive columns after each reiteration of the loop, but am unsure how to do so other than assigning each pass to a separate variable and then formating the printing-a very messy deal. Any ideas? my code thus far:
open (FILE, "brca1_prettybase.txt"); our @data=''; @data = <FILE>; close (FILE); $i=0; while ($i <=300) { #for ($i=0; $i<=89; $i++) { # @zz=$data[$i]; @zz=splice(@data, 0, 89); foreach $_(@zz){ $a=''; $b=''; $c=''; $d=''; ($a, $b, $c, $d) = split(/[\t]/,$_); $qq=join ('/',$c,$d); push (@qqq, $qq); #print "$b\n"; } print "$a\n"; print @qqq; $i++; }
Fixed code tags - dvergin 2003-07-07

Replies are listed 'Best First'.
Re: Printing in succesive columns...
by Zaxo (Archbishop) on Jul 07, 2003 at 20:16 UTC

    If you want to avoid formats, the handiest way to get fixed width columns is with pack. Given $width and $cols,

    while (@data) { print pack( "A${width}" x $cols, splice @data, 0, $cols), $/; }
    While that may not be the print format you want, it illustrates how to control this loop in an index-free way.

    After Compline,
    Zaxo

Re: Printing in succesive columns...
by TomDLux (Vicar) on Jul 07, 2003 at 21:17 UTC

    Personally, I would do it with printf. You can look up formatting options in any C book. (There are quite a few; like pack/unpack, it's like a programming language of it's own.)

    $margin = 10; printf "'%${margin}s'\n", 'x'; __output__ ' x'

    You don't need references to $_ in the foreach or split lines. The whole point about the default variable is that you can leave out the variable, and focus on the operation. Fewer implementation details, more result.

    In related news,.... since you don't need $a or $b, just tell Perl to discard them by taking a slice of the split result:

    ... (split( /\t/ ))[2,3];

    But you only need $c & $d for two lines, to join back together. So why not merge the two lines:

    ... join '/', (split( /\t/ ))[2,3];

    You only use $qq to add the result of the join into @qqq, so save another line:

    push @qqq, join '/', (split( /\t/ ))[2,3];

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      I also like printf, and your solution can also be written as:
      printf "'%*s'\n",$margin,'x';
      where the * means take the value from the next argument in the list, a feature which I've always found pretty handy.

      --
      I'd like to be able to assign to an luser