Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: format conversion, please help

by hdb (Parson)
on Apr 06, 2013 at 19:09 UTC ( #1027294=note: print w/ replies, xml ) Need Help??


in reply to format conversion, please help

How about this:

use strict; use warnings; my $letters = <DATA>; my @letters = $letters =~ /\w+/g; my %out; while(<DATA>) { next if /SampleID/; chomp; my @recs = split /\s+/; my $ctr = 2; for my $letter (@letters) { $out{$letter} .= "$letter\t".(join "\t", @recs[0,1,$ctr++])."\n"; } } for my $letter (@letters) { print $out{$letter}; } __DATA__ A C F SampleID Time ObsConc ObsConc ObsConc 5 24 2.27E+06 687.02 32521.94 5 168 1.92E+06 525.02 22198.44 5 12 2.94E+06 896.39 41331.61 5 -0.5 2.23E+06 942.34 40616.49 5 8 4.03E+06 1371.32 45863.69 6 24 1.02E+06 1057.89 46341.04 6 168 3.14E+06 4987.32 42166.08

This assumes that the line with A C F is potentially variable and due to change. Otherwise it could be further simplified by hardcoding it. It also assumes that you always repeat the first two columns.

It builds the output fully in memory, which might be a problem if you have HUGE input. In order to minimize memory consumption, one could use temporary files for each of the letters and merge them later.

UPDATE: No statement about the "ugliness" of this or other code is implied...


Comment on Re: format conversion, please help
Download Code
Re^2: format conversion, please help
by david_lyon (Sexton) on Apr 06, 2013 at 19:37 UTC
    This works very nicely and is good looking perl code... Thanks!

      Another thought: if the order of lines in the output does not matter it would be simpler.

      use strict; use warnings; my $letters = <DATA>; my @letters = $letters =~ /\w+/g; while(<DATA>) { next if /SampleID/; chomp; my @recs = split /\s+/; my $ctr = 2; for my $letter (@letters) { print "$letter\t".(join "\t", @recs[0,1,$ctr++])."\n"; } } __DATA__ A C F SampleID Time ObsConc ObsConc ObsConc 5 24 2.27E+06 687.02 32521.94 5 168 1.92E+06 525.02 22198.44 5 12 2.94E+06 896.39 41331.61 5 -0.5 2.23E+06 942.34 40616.49 5 8 4.03E+06 1371.32 45863.69 6 24 1.02E+06 1057.89 46341.04 6 168 3.14E+06 4987.32 42166.08

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-08-22 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (158 votes), past polls