Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
The key thing to do is to have your data sorted by your key (in this case the first column of your data). Then you only need to keep a small amount of data in memory.

By the way, in the case where the column headers of your input files are not all different (except for the first), your program's output depends on the order the files are named in the command line.

Here's one way to process the data. I haven't tested this at all except to check that it passes perl -c.

#!/usr/bin/perl -w use strict; use warnings; my @fileHeaders; my %usedHeaders; my $sortname = "sortfile.tmp"; # If you have more than one disk, consider putting # a disk different from most of your data. # Beware of disk space. open SORTFILE, ">", $sortname or die "Cannot create $sortname: $!\n"; # first pass, write to sortfile foreach my $filename (@ARGV) { open GSE, "<", $filename or die "Cannot open $filename: $!\n"; my $headerline = <GSE>; chomp($headerline); $headerline =~ s/\r$//; my @thisfileHeaders = split /\t/, $headerline; shift @thisfileHeaders; push @fileHeaders, \@thisfileHeaders; my $filekey = sprintf "%04d", $#fileHeaders; while(<GSE>) { y/\r//d; s/^([^\t]*)/$1\t$filekey/; print SORTFILE $_ or die "print SORTFILE failed: $!\n"; } if ($. > 1) { # if any data past headers $usedHeaders{$_}++ for @thisfileHeaders; } close GSE; } close SORTFILE or die "close SORTFILE failed: $!\n"; # Yes, close can fail. Buffered I/O, for one thing system("sort -o sortfile.tmp sortfile.tmp") == 0 or die "Sort failed!\n"; # second pass; consolidate data from multiple files my $prevkey; open SORTFILE, "<", $sortname or die "Cannot open $sortname: $!\n"; my @samples = sort keys %usedHeaders; print "Probe\t".join("\t",@samples)."\n"; my %data; while (my $ligne = <SORTFILE>) { chomp($ligne); my @t = split(/\t/, $ligne); my $probe = shift @t; my $filenum = shift @t; if (!$prevkey || $probe ne $prevkey) { dumpdata(); $prevkey = $probe; } @data{@{$fileHeaders[$filenum]}} = @t; } dumpdata(); sub dumpdata { if (defined($prevkey)) { print "$prevkey\t", join("\t", @data{@samples}), "\n"; } $data{$_} = "" for @samples; #initialize to blanks so no warnings on printing undef }
Update: small edits.

In reply to Re: How to deal with Huge data by Thelonius
in thread How to deal with Huge data by Marsel

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [vickycanada]: Hi, Can anyone please help me with this - 1203954

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (9)
    As of 2017-11-21 21:44 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (312 votes). Check out past polls.