Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

how to use printf correctly...

by bioinformatics (Friar)
on Jul 09, 2003 at 17:16 UTC ( [id://272748]=perlquestion: print w/replies, xml ) Need Help??

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

I had an earlier question regarding printing data in succesive columns. It was suggested that I use printf to do this, but I'm not doing something right, though I have no clue what. When looping my program, all the data from a particular array (@qqq) prints out in a column. Fine, except after the reiterations of this loop, the column only gets longer to make one long column. It may perhaps be easier to sort things and print the columns one row at a time. I can't figure out how to do that effectively either. The original text file looks something like this:
000217 P008 N N
000217 P009 G G
000217 P010 A A
000217 P011 A G
000217 P012 A A
000217 P013 A G
000217 P014 A A
000217 P015 A G
000217 P016 A G
times about 10,000. After joining the letters, I loop through the first 90; this 90x loop is then reiterated about 301 times, hopefully to bring me 301 separate columns side by side. Easier said than done!! Ideas anyone?
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++; }

Replies are listed 'Best First'.
Re: how to use printf correctly...
by BrowserUk (Patriarch) on Jul 09, 2003 at 18:53 UTC

    If I understand you correctly, you have an input file with four fields per line. You want to discard the first two, and join the last two like this "G/A". You then want to print every 90th of these as a new column on each line to produce 90 lines of 301 columns. (I'm ignoring the fact that 90*301 != 10,000 :).

    This sample code shows one way to do this...I've done 3 lines of 34 to save a little space:)

    #! perl -slw use strict; # Read the file, split into fields, discard the first 2 # join fields 2/3 as required for output. my @data = map{ join '/', (split ' ')[2,3] } <DATA>; my @lines; # Array in which to accumulate the output. # build each output line for my $line ( 0 .. 2 ) { # 0 .. 89 in your app # by steping through the input and appending every nth element to +the output for my $col ( 0 .. 33 ) { # 0 .. 300 in your app $lines[ $line ] .= $data[ $line * 34 + $col ] . ' '; } } # display the output. print for @lines;

    Output

    P:\test>junk N/N G/G A/A A/G A/A A/G A/A A/G A/G N/N G/G A/A A/G A/A A/G A/A A/G A/ +G N/N G/G A/A A/G A/A A/G A/A A/G A/G N/N G/G A/A A/G A/A A/G A/A N/N G/G A/A A/G A/A A/G A/A A/G A/G N/N G/G A/A A/G A/A A/G A/A A/G A/ +G N/N G/G A/A A/G A/A A/G A/A A/G A/G N/N G/G A/A A/G A/A A/G A/A N/N G/G A/A A/G A/A A/G A/A A/G A/G N/N G/G A/A A/G A/A A/G A/A A/G A/ +G N/N G/G A/A A/G A/A A/G A/A A/G A/G N/N G/G A/A A/G A/A A/G A/A

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: how to use printf correctly...
by pzbagel (Chaplain) on Jul 09, 2003 at 18:44 UTC

    Even though your subject is printf, you ask a much larger question in regards to converting your data into columns going across rather than down. The easiest option by far (assuming you have the memory to do so) would be to create a new data structure in the form of a 2 dimensional array and then you can easily print it out using some nested for loops:

    #!/usr/bin/perl -w use strict; use Data::Dumper; our @qqq=(1..100); sub print_col { my ($rows, $qref)=@_; my @cols=(); my $count=0; while($count<=$#$qref) { #This for loop creates the 2D array for (0..$rows-1) { push @{$cols[$_]}, $qref->[$count]; $count++; #This makes sure to stop when we run out of data last if $count > $#$qref; } } #Data::Dumper just in here for testing, love that module #print Dumper(\@cols); #This for loop prints it, notice I use the quoted array syntax + #which causes print to output the array with spaces in between + # each element. However, this is the perfect place to put #another for loop or a map statement and printf each element #with the correct width and other formatting you want. for (0..$#cols){ print "@{$cols[$_]}\n"; } } print_col(shift, \@qqq); ###################### ## Output $ ./testcols.pl 25 1 26 51 76 2 27 52 77 3 28 53 78 4 29 54 79 5 30 55 80 6 31 56 81 7 32 57 82 8 33 58 83 9 34 59 84 10 35 60 85 11 36 61 86 12 37 62 87 13 38 63 88 14 39 64 89 15 40 65 90 16 41 66 91 17 42 67 92 18 43 68 93 19 44 69 94 20 45 70 95 21 46 71 96 22 47 72 97 23 48 73 98 24 49 74 99 25 50 75 100

    HTH

Re: how to use printf correctly...
by hardburn (Abbot) on Jul 09, 2003 at 17:40 UTC

    If I understand correctly, you want fixed-width fields to be printed out, with the data aligned nice and pretty. In other words, you want format().

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: how to use printf correctly...
by jjhorner (Hermit) on Jul 09, 2003 at 17:33 UTC

    I'm not exactly sure what you are asking in terms of output.

    You want to import 90 lines, strip off the first two fields separated by a 'tab' character, join the last two with a '/', push them onto an array, print the first field, print the whole array, then cycle back for the next 90?

    Or do you want to build 301 X/X columns side by side? Something like this:

    000217
    N/N
    G/G
    A/A
    A/G
    A/A
    A/G
    A/A
    A/G
    A/G
    (until 90 items are listed),
    

    Then cycle back and grab another 90, and add to the above by adding another column onto the X/X list?

    Let me know. I'm kind of unsure about what you are asking.

    J. J. Horner 
    CISSP,CCNA,CHSS,CHP,blah,blah,blah
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 04:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found